Skip to content

Commit

Permalink
Use Rack::MockRequest for correct router recognize on hanami app
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei committed Apr 30, 2024
1 parent 9650075 commit 8817b30
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
7 changes: 0 additions & 7 deletions lib/rspec/openapi/extractors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,4 @@

# 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
13 changes: 3 additions & 10 deletions lib/rspec/openapi/extractors/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def add_route(route)
def call(verb, path)
route = routes.find { |r| r.http_method == verb && r.path == path }

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

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

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

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

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

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

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

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

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

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

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

Expand Down
59 changes: 34 additions & 25 deletions spec/apps/hanami/doc/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,23 @@
}
}
},
"/sites/abc123": {
"/sites/{name}": {
"get": {
"summary": "show",
"tags": [
"Site"
],
"parameters": [
{
"name": "name",
"in": "path",
"required": true,
"schema": {
"type": "string"
},
"example": "no-such"
}
],
"responses": {
"200": {
"description": "finds a site",
Expand All @@ -454,13 +469,7 @@
}
}
}
}
}
}
},
"/sites/no-such": {
"get": {
"responses": {
},
"404": {
"description": "raises not found",
"content": {
Expand Down Expand Up @@ -815,6 +824,23 @@
"example": 1
}
],
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"properties": {
"no_content": {
"type": "string"
}
}
},
"example": {
"no_content": "true"
}
}
}
},
"responses": {
"200": {
"description": "returns a table",
Expand Down Expand Up @@ -891,23 +917,6 @@
"202": {
"description": "returns no content if specified"
}
},
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"properties": {
"no_content": {
"type": "string"
}
}
},
"example": {
"no_content": "true"
}
}
}
}
},
"get": {
Expand Down
15 changes: 11 additions & 4 deletions spec/apps/hanami/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,18 @@ paths:
schema:
type: string
example: ''
"/sites/abc123":
"/sites/{name}":
get:
summary: show
tags:
- Site
parameters:
- name: name
in: path
required: true
schema:
type: string
example: no-such
responses:
'200':
description: finds a site
Expand All @@ -281,9 +291,6 @@ paths:
- name
example:
name: abc123
"/sites/no-such":
get:
responses:
'404':
description: raises not found
content:
Expand Down

0 comments on commit 8817b30

Please sign in to comment.