Skip to content

Commit afd69ad

Browse files
AlexeyMatskevichAleksei
andauthored
Use Rack::MockRequest for correct router recognize on hanami app (#231)
Co-authored-by: Aleksei <[email protected]>
1 parent 9650075 commit afd69ad

File tree

4 files changed

+48
-46
lines changed

4 files changed

+48
-46
lines changed

lib/rspec/openapi/extractors.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,4 @@
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
125
end

lib/rspec/openapi/extractors/hanami.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ def add_route(route)
1919
def call(verb, path)
2020
route = routes.find { |r| r.http_method == verb && r.path == path }
2121

22-
if route.nil?
23-
# FIXME: This is a hack to pass `/sites/***` in testing
24-
{}
25-
elsif route.to.is_a?(Proc)
22+
if route.to.is_a?(Proc)
2623
{
2724
tags: [],
2825
summary: "#{verb} #{path}",
@@ -55,7 +52,7 @@ class << RSpec::OpenAPI::Extractors::Hanami = Object.new
5552
# @param [RSpec::Core::Example] example
5653
# @return Array
5754
def request_attributes(request, example)
58-
route = Hanami.app.router.recognize(request.path, method: request.method)
55+
route = Hanami.app.router.recognize(Rack::MockRequest.env_for(request.path, method: request.method))
5956

6057
return RSpec::OpenAPI::Extractors::Rack.request_attributes(request, example) unless route.routable?
6158

@@ -69,7 +66,7 @@ def request_attributes(request, example)
6966
deprecated = metadata[:deprecated]
7067
path = request.path
7168

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

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

@@ -95,8 +92,6 @@ def add_id(path, route)
9592
return path if route.params.empty?
9693

9794
route.params.each_pair do |key, value|
98-
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)
99-
10095
path = path.sub("/#{value}", "/:#{key}")
10196
end
10297

@@ -107,8 +102,6 @@ def add_openapi_id(path, route)
107102
return path if route.params.empty?
108103

109104
route.params.each_pair do |key, value|
110-
next unless RSpec::OpenAPI::Extractors.number_or_nil(value)
111-
112105
path = path.sub("/#{value}", "/{#{key}}")
113106
end
114107

spec/apps/hanami/doc/openapi.json

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,23 @@
431431
}
432432
}
433433
},
434-
"/sites/abc123": {
434+
"/sites/{name}": {
435435
"get": {
436+
"summary": "show",
437+
"tags": [
438+
"Site"
439+
],
440+
"parameters": [
441+
{
442+
"name": "name",
443+
"in": "path",
444+
"required": true,
445+
"schema": {
446+
"type": "string"
447+
},
448+
"example": "no-such"
449+
}
450+
],
436451
"responses": {
437452
"200": {
438453
"description": "finds a site",
@@ -454,13 +469,7 @@
454469
}
455470
}
456471
}
457-
}
458-
}
459-
}
460-
},
461-
"/sites/no-such": {
462-
"get": {
463-
"responses": {
472+
},
464473
"404": {
465474
"description": "raises not found",
466475
"content": {
@@ -815,6 +824,23 @@
815824
"example": 1
816825
}
817826
],
827+
"requestBody": {
828+
"content": {
829+
"application/x-www-form-urlencoded": {
830+
"schema": {
831+
"type": "object",
832+
"properties": {
833+
"no_content": {
834+
"type": "string"
835+
}
836+
}
837+
},
838+
"example": {
839+
"no_content": "true"
840+
}
841+
}
842+
}
843+
},
818844
"responses": {
819845
"200": {
820846
"description": "returns a table",
@@ -891,23 +917,6 @@
891917
"202": {
892918
"description": "returns no content if specified"
893919
}
894-
},
895-
"requestBody": {
896-
"content": {
897-
"application/x-www-form-urlencoded": {
898-
"schema": {
899-
"type": "object",
900-
"properties": {
901-
"no_content": {
902-
"type": "string"
903-
}
904-
}
905-
},
906-
"example": {
907-
"no_content": "true"
908-
}
909-
}
910-
}
911920
}
912921
},
913922
"get": {

spec/apps/hanami/doc/openapi.yaml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,18 @@ paths:
265265
schema:
266266
type: string
267267
example: ''
268-
"/sites/abc123":
268+
"/sites/{name}":
269269
get:
270+
summary: show
271+
tags:
272+
- Site
273+
parameters:
274+
- name: name
275+
in: path
276+
required: true
277+
schema:
278+
type: string
279+
example: no-such
270280
responses:
271281
'200':
272282
description: finds a site
@@ -281,9 +291,6 @@ paths:
281291
- name
282292
example:
283293
name: abc123
284-
"/sites/no-such":
285-
get:
286-
responses:
287294
'404':
288295
description: raises not found
289296
content:

0 commit comments

Comments
 (0)