From afd69ad63b14e597fafd73ab2c7350efcba6d6bb Mon Sep 17 00:00:00 2001 From: Alexey Matskevich <45947106+AlexeyMatskevich@users.noreply.github.com> Date: Wed, 1 May 2024 14:17:44 +0500 Subject: [PATCH] Use Rack::MockRequest for correct router recognize on hanami app (#231) Co-authored-by: Aleksei <9ceb2990-3744-4629-82f3-19bc5c80b3a2@mackevich.addymail.com> --- lib/rspec/openapi/extractors.rb | 7 --- lib/rspec/openapi/extractors/hanami.rb | 13 ++---- spec/apps/hanami/doc/openapi.json | 59 +++++++++++++++----------- spec/apps/hanami/doc/openapi.yaml | 15 +++++-- 4 files changed, 48 insertions(+), 46 deletions(-) diff --git a/lib/rspec/openapi/extractors.rb b/lib/rspec/openapi/extractors.rb index 52c686a..c8322ae 100644 --- a/lib/rspec/openapi/extractors.rb +++ b/lib/rspec/openapi/extractors.rb @@ -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 diff --git a/lib/rspec/openapi/extractors/hanami.rb b/lib/rspec/openapi/extractors/hanami.rb index 2884fc5..3f1155d 100644 --- a/lib/rspec/openapi/extractors/hanami.rb +++ b/lib/rspec/openapi/extractors/hanami.rb @@ -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}", @@ -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? @@ -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)) @@ -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 @@ -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 diff --git a/spec/apps/hanami/doc/openapi.json b/spec/apps/hanami/doc/openapi.json index 430ff02..0de709c 100644 --- a/spec/apps/hanami/doc/openapi.json +++ b/spec/apps/hanami/doc/openapi.json @@ -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", @@ -454,13 +469,7 @@ } } } - } - } - } - }, - "/sites/no-such": { - "get": { - "responses": { + }, "404": { "description": "raises not found", "content": { @@ -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", @@ -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": { diff --git a/spec/apps/hanami/doc/openapi.yaml b/spec/apps/hanami/doc/openapi.yaml index 8ef030f..9989765 100644 --- a/spec/apps/hanami/doc/openapi.yaml +++ b/spec/apps/hanami/doc/openapi.yaml @@ -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 @@ -281,9 +291,6 @@ paths: - name example: name: abc123 - "/sites/no-such": - get: - responses: '404': description: raises not found content: