From 3f01d03b7a28b088a52313d8d264f29aeacd41fa Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Mon, 20 Nov 2023 18:03:22 -0500 Subject: [PATCH] method missing to_ary (#2370) * Add to_ary nil * Add changelog * Remove method_missing and warning * Update CHANGELOG.md * Add upgrading notes Replace route_params by params * Update README.md --- CHANGELOG.md | 1 + README.md | 6 +++--- UPGRADING.md | 8 ++++++++ lib/grape/router/route.rb | 37 ------------------------------------- spec/grape/api_spec.rb | 3 +-- 5 files changed, 13 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4f0993148..40acedb2af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ #### Fixes +* [#2370](https://github.com/ruby-grape/grape/pull/2370): Remove route_xyz method_missing deprecation - [@ericproulx](https://github.com/ericproulx). * [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia). * [#2373](https://github.com/ruby-grape/grape/pull/2373): Fix markdown files for following 1-line format - [@jcagarcia](https://github.com/jcagarcia). * Your contribution here. diff --git a/README.md b/README.md index 0e38b56635..e6b68bc9d3 100644 --- a/README.md +++ b/README.md @@ -3352,7 +3352,7 @@ You can access the controller params, headers, and helpers through the context w Grape routes can be reflected at runtime. This can notably be useful for generating documentation. -Grape exposes arrays of API versions and compiled routes. Each route contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`, `route_path` and `route_params`. You can add custom route settings to the route metadata with `route_setting`. +Grape exposes arrays of API versions and compiled routes. Each route contains a `prefix`, `version`, `namespace`, `method` and `params`. You can add custom route settings to the route metadata with `route_setting`. ```ruby class TwitterAPI < Grape::API @@ -3375,7 +3375,7 @@ TwitterAPI::routes[0].description # => 'Includes custom settings.' TwitterAPI::routes[0].settings[:custom] # => { key: 'value' } ``` -Note that `Route#route_xyz` methods have been deprecated since 0.15.0. +Note that `Route#route_xyz` methods have been deprecated since 0.15.0 and removed since 2.0.1. Please use `Route#xyz` instead. @@ -3395,7 +3395,7 @@ class MyAPI < Grape::API requires :id, type: Integer, desc: 'Identity.' end get 'params/:id' do - route.route_params[params[:id]] # yields the parameter description + route.params[params[:id]] # yields the parameter description end end ``` diff --git a/UPGRADING.md b/UPGRADING.md index 912bbb41bd..4cb1d4c62a 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,14 @@ Upgrading Grape =============== +### Upgrading to >= 2.0.1 + +#### Grape::Router::Route.route_xxx methods have been removed + +- `route_method` is accessible through `request_method` +- `route_path` is accessible through `path` +- Any other `route_xyz` are accessible through `options[xyz]` + ### Upgrading to >= 2.0.0 #### Headers diff --git a/lib/grape/router/route.rb b/lib/grape/router/route.rb index d5600df1c9..ea49bac780 100644 --- a/lib/grape/router/route.rb +++ b/lib/grape/router/route.rb @@ -3,13 +3,10 @@ require 'grape/router/pattern' require 'grape/router/attribute_translator' require 'forwardable' -require 'pathname' module Grape class Router class Route - ROUTE_ATTRIBUTE_REGEXP = /route_([_a-zA-Z]\w*)/.freeze - SOURCE_LOCATION_REGEXP = /^(.*?):(\d+?)(?::in `.+?')?$/.freeze FIXED_NAMED_CAPTURES = %w[format version].freeze attr_accessor :pattern, :translator, :app, :index, :options @@ -20,31 +17,6 @@ class Route def_delegators :pattern, :path, :origin delegate Grape::Router::AttributeTranslator::ROUTE_ATTRIBUTES => :attributes - def method_missing(method_id, *arguments) - match = ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s) - if match - method_name = match.captures.last.to_sym - warn_route_methods(method_name, caller(1).shift) - @options[method_name] - else - super - end - end - - def respond_to_missing?(method_id, _) - ROUTE_ATTRIBUTE_REGEXP.match?(method_id.to_s) - end - - def route_method - warn_route_methods(:method, caller(1).shift, :request_method) - request_method - end - - def route_path - warn_route_methods(:path, caller(1).shift) - pattern.path - end - def initialize(method, pattern, **options) method_s = method.to_s method_upcase = Grape::Http::Headers.find_supported_method(method_s) || method_s.upcase @@ -77,15 +49,6 @@ def params(input = nil) parsed ? parsed.delete_if { |_, value| value.nil? }.symbolize_keys : {} end end - - private - - def warn_route_methods(name, location, expected = nil) - path, line = *location.scan(SOURCE_LOCATION_REGEXP).first - path = File.realpath(path) if Pathname.new(path).relative? - expected ||= name - Grape.deprecator.warn("#{path}:#{line}: The route_xxx methods such as route_#{name} have been deprecated, please use #{expected}.") - end end end end diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 161b561cbb..b67dcbe24e 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -3050,7 +3050,6 @@ def static expect(subject.routes.length).to eq(1) route = subject.routes.first expect(route.description).to eq('first method') - expect(route.route_foo).to be_nil expect(route.params).to eq({}) expect(route.options).to be_a(Hash) end @@ -3095,7 +3094,7 @@ def static get 'second' end expect(subject.routes.map do |route| - { description: route.description, foo: route.route_foo, params: route.params } + { description: route.description, foo: route.options[:foo], params: route.params } end).to eq [ { description: 'ns second', foo: 'bar', params: {} } ]