Skip to content

Commit 556ab5f

Browse files
authored
regression: Symbol parameters in routing causes error on Rails (#221)
1 parent 4e3b8d2 commit 556ab5f

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

.rubocop_todo.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-04-12 14:06:44 UTC using RuboCop version 1.62.1.
3+
# on 2024-04-20 21:25:22 UTC using RuboCop version 1.62.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 13
9+
# Offense count: 14
1010
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1111
Metrics/AbcSize:
12-
Max: 47
12+
Max: 46
1313

1414
# Offense count: 1
1515
# Configuration parameters: CountComments, CountAsOne.
@@ -19,7 +19,7 @@ Metrics/ClassLength:
1919
# Offense count: 9
2020
# Configuration parameters: AllowedMethods, AllowedPatterns.
2121
Metrics/CyclomaticComplexity:
22-
Max: 12
22+
Max: 13
2323

2424
# Offense count: 22
2525
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
@@ -29,7 +29,7 @@ Metrics/MethodLength:
2929
# Offense count: 5
3030
# Configuration parameters: AllowedMethods, AllowedPatterns.
3131
Metrics/PerceivedComplexity:
32-
Max: 12
32+
Max: 13
3333

3434
# Offense count: 1
3535
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.

lib/rspec/openapi/extractors.rb

+7
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@
22

33
# Create namespace
44
module RSpec::OpenAPI::Extractors
5+
# @param [String, Symbol] path_parameter
6+
# @return [Integer, nil]
7+
def self.number_or_nil(path_parameter)
8+
Integer(path_parameter.to_s || '')
9+
rescue ArgumentError
10+
nil
11+
end
512
end

lib/rspec/openapi/extractors/hanami.rb

+3-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def request_attributes(request, example)
6666
deprecated = metadata[:deprecated]
6767
path = request.path
6868

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

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

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

9494
route.params.each_pair do |key, value|
95-
next unless number_or_nil(value)
95+
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)
9696

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

106106
route.params.each_pair do |key, value|
107-
next unless number_or_nil(value)
107+
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)
108108

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

112112
path
113113
end
114-
115-
def number_or_nil(string)
116-
Integer(string || '')
117-
rescue ArgumentError
118-
nil
119-
end
120114
end

lib/rspec/openapi/extractors/rails.rb

+1-7
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,11 @@ def find_rails_route(request, app: Rails.application, path_prefix: '')
6565

6666
def add_id(path, parameters)
6767
parameters.each_pair do |key, value|
68-
next unless number_or_nil(value)
68+
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)
6969

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

7373
path
7474
end
75-
76-
def number_or_nil(string)
77-
Integer(string || '')
78-
rescue ArgumentError
79-
nil
80-
end
8175
end

spec/apps/rails/config/routes.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

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

7-
get '/pages' => 'pages#get'
7+
defaults format: :html do
8+
get '/pages' => 'pages#get'
9+
end
810

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

0 commit comments

Comments
 (0)