File tree Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Expand file tree Collapse file tree 3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,10 @@ module OpenapiFirst
15
15
16
16
# Key in rack to find instance of Request
17
17
REQUEST = 'openapi.request'
18
+
19
+ # Key in rack env that stores the alternative path used for schema matching
20
+ RACK_KEY_PATH_TO_MATCH = 'openapi.path_to_match'
21
+
18
22
FAILURE = :openapi_first_validation_failure
19
23
20
24
# @return [Configuration]
@@ -31,6 +35,13 @@ def self.configure
31
35
ERROR_RESPONSES = { } # rubocop:disable Style/MutableConstant
32
36
private_constant :ERROR_RESPONSES
33
37
38
+ # Retrieve the path used for schema matching.
39
+ #
40
+ # @param [Rack::Request] rack_request
41
+ def self . get_path_to_match ( rack_request )
42
+ rack_request . env [ RACK_KEY_PATH_TO_MATCH ] || rack_request . path
43
+ end
44
+
34
45
# Register an error response class
35
46
# @param name [Symbol]
36
47
# @param klass [Class] A class that includes / implements OpenapiFirst::ErrorResponse
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ def initialize(contents, filepath = nil)
45
45
# @param [Boolean] raise_error Whether to raise an error if validation fails.
46
46
# @return [ValidatedRequest] The validated request object.
47
47
def validate_request ( request , raise_error : false )
48
- route = @router . match ( request . request_method , request . path , content_type : request . content_type )
48
+ route = @router . match ( request . request_method , OpenapiFirst . get_path_to_match ( request ) , content_type : request . content_type )
49
49
if route . error
50
50
ValidatedRequest . new ( request , error : route . error )
51
51
else
@@ -62,7 +62,7 @@ def validate_request(request, raise_error: false)
62
62
# @param raise_error [Boolean] Whethir to raise an error if validation fails.
63
63
# @return [ValidatedResponse] The validated response object.
64
64
def validate_response ( rack_request , rack_response , raise_error : false )
65
- route = @router . match ( rack_request . request_method , rack_request . path , content_type : rack_request . content_type )
65
+ route = @router . match ( rack_request . request_method , OpenapiFirst . get_path_to_match ( rack_request ) , content_type : rack_request . content_type )
66
66
return if route . error # Skip response validation for unknown requests
67
67
68
68
response_match = route . match_response ( status : rack_response . status , content_type : rack_response . content_type )
Original file line number Diff line number Diff line change @@ -213,6 +213,20 @@ def build_request(path, method: 'GET')
213
213
) . to be_valid
214
214
end
215
215
end
216
+
217
+ context 'with an alternate path used for schema matching' do
218
+ let ( :request ) {
219
+ build_request ( '/prefix/stuff/42' ) . tap { |req |
220
+ req . env [ OpenapiFirst ::RACK_KEY_PATH_TO_MATCH ] = '/stuff/42'
221
+ }
222
+ }
223
+
224
+ it 'returns a valid request' do
225
+ validated = definition . validate_request ( request )
226
+ expect ( validated ) . to be_valid
227
+ expect ( validated . parsed_path_parameters ) . to eq ( { 'id' => 42 } )
228
+ end
229
+ end
216
230
end
217
231
218
232
describe '#validate_response' do
@@ -296,6 +310,21 @@ def build_request(path, method: 'GET')
296
310
end
297
311
end
298
312
end
313
+
314
+ context 'with an alternate path used for schema matching' do
315
+ let ( :request ) {
316
+ build_request ( '/prefix/stuff/42' ) . tap { |req |
317
+ req . env [ OpenapiFirst ::RACK_KEY_PATH_TO_MATCH ] = '/stuff'
318
+ }
319
+ }
320
+ let ( :response ) { Rack ::Response . new ( JSON . generate ( { 'id' => 42 } ) , 200 , { 'Content-Type' => 'application/json' } ) }
321
+
322
+ it 'returns a valid response' do
323
+ validated = definition . validate_response ( request , response )
324
+ expect ( validated ) . to be_valid
325
+ expect ( validated . parsed_body ) . to eq ( { 'id' => 42 } )
326
+ end
327
+ end
299
328
end
300
329
301
330
describe '#routes' do
You can’t perform that action at this time.
0 commit comments