Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regression: Symbol parameters in routing causes error on Rails #221

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-04-12 14:06:44 UTC using RuboCop version 1.62.1.
# on 2024-04-20 21:25:22 UTC using RuboCop version 1.62.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 13
# Offense count: 14
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 47
Max: 46

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Expand All @@ -19,7 +19,7 @@ Metrics/ClassLength:
# Offense count: 9
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 12
Max: 13

# Offense count: 22
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Expand All @@ -29,7 +29,7 @@ Metrics/MethodLength:
# Offense count: 5
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 12
Max: 13

# Offense count: 1
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
Expand Down
7 changes: 7 additions & 0 deletions lib/rspec/openapi/extractors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@

# Create namespace
module RSpec::OpenAPI::Extractors
# @param [String, Symbol] path_parameter
# @return [Integer, nil]
def self.number_or_nil(path_parameter)
Integer(path_parameter.to_s || '')
rescue ArgumentError
nil
end
end
12 changes: 3 additions & 9 deletions lib/rspec/openapi/extractors/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def request_attributes(request, example)
deprecated = metadata[:deprecated]
path = request.path

raw_path_params = route.params.filter { |_key, value| number_or_nil(value) }
raw_path_params = route.params.filter { |_key, value| RSpec::OpenAPI::Extractors.number_or_nil(value) }

result = InspectorAnalyzer.call(request.method, add_id(path, route))

Expand All @@ -92,7 +92,7 @@ def add_id(path, route)
return path if route.params.empty?

route.params.each_pair do |key, value|
next unless number_or_nil(value)
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)

path = path.sub("/#{value}", "/:#{key}")
end
Expand All @@ -104,17 +104,11 @@ def add_openapi_id(path, route)
return path if route.params.empty?

route.params.each_pair do |key, value|
next unless number_or_nil(value)
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)

path = path.sub("/#{value}", "/{#{key}}")
end

path
end

def number_or_nil(string)
Integer(string || '')
rescue ArgumentError
nil
end
end
8 changes: 1 addition & 7 deletions lib/rspec/openapi/extractors/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,11 @@ def find_rails_route(request, app: Rails.application, path_prefix: '')

def add_id(path, parameters)
parameters.each_pair do |key, value|
next unless number_or_nil(value)
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)

path = path.sub("/#{value}", "/:#{key}")
end

path
end

def number_or_nil(string)
Integer(string || '')
rescue ArgumentError
nil
end
end
4 changes: 3 additions & 1 deletion spec/apps/rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

get '/my_engine/test' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['ANOTHER TEST']] }

get '/pages' => 'pages#get'
defaults format: :html do
get '/pages' => 'pages#get'
end

defaults format: 'json' do
resources :tables, only: [:index, :show, :create, :update, :destroy]
Expand Down