Skip to content

Commit 099958e

Browse files
authored
Merge pull request #255 from rhiroe/fix/Inherit-openapi-metadata
2 parents 80c5b58 + b6f3575 commit 099958e

22 files changed

+2222
-28
lines changed

lib/rspec/openapi/extractors/hanami.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def request_attributes(request, example)
5656

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

59-
metadata = example.metadata[:openapi] || {}
59+
metadata = merge_openapi_metadata(example.metadata)
6060
summary = metadata[:summary] || RSpec::OpenAPI.summary_builder.call(example)
6161
tags = metadata[:tags] || RSpec::OpenAPI.tags_builder.call(example)
6262
formats = metadata[:formats] || RSpec::OpenAPI.formats_builder.curry.call(example)
@@ -100,6 +100,26 @@ def request_response(context)
100100
[request, response]
101101
end
102102

103+
private
104+
105+
def merge_openapi_metadata(metadata)
106+
collect_openapi_metadata(metadata).reduce({}, &:merge)
107+
end
108+
109+
def collect_openapi_metadata(metadata)
110+
[].tap do |result|
111+
current = metadata
112+
113+
while current
114+
[current[:example_group], current].each do |meta|
115+
result.unshift(meta[:openapi]) if meta&.dig(:openapi)
116+
end
117+
118+
current = current[:parent_example_group]
119+
end
120+
end
121+
end
122+
103123
def add_id(path, route)
104124
return path if route.params.empty?
105125

lib/rspec/openapi/extractors/rack.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class << RSpec::OpenAPI::Extractors::Rack = Object.new
66
# @param [RSpec::Core::Example] example
77
# @return Array
88
def request_attributes(request, example)
9-
metadata = example.metadata[:openapi] || {}
9+
metadata = merge_openapi_metadata(example.metadata)
1010
summary = metadata[:summary] || RSpec::OpenAPI.summary_builder.call(example)
1111
tags = metadata[:tags] || RSpec::OpenAPI.tags_builder.call(example)
1212
formats = metadata[:formats] || RSpec::OpenAPI.formats_builder.curry.call(example)
@@ -40,4 +40,24 @@ def request_response(context)
4040

4141
[request, response]
4242
end
43+
44+
private
45+
46+
def merge_openapi_metadata(metadata)
47+
collect_openapi_metadata(metadata).reduce({}, &:merge)
48+
end
49+
50+
def collect_openapi_metadata(metadata)
51+
[].tap do |result|
52+
current = metadata
53+
54+
while current
55+
[current[:example_group], current].each do |meta|
56+
result.unshift(meta[:openapi]) if meta&.dig(:openapi)
57+
end
58+
59+
current = current[:parent_example_group]
60+
end
61+
end
62+
end
4363
end

lib/rspec/openapi/extractors/rails.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def request_attributes(request, example)
1616

1717
raise "No route matched for #{fixed_request.request_method} #{fixed_request.path_info}" if route.nil?
1818

19-
metadata = example.metadata[:openapi] || {}
19+
metadata = merge_openapi_metadata(example.metadata)
2020
summary = metadata[:summary] || RSpec::OpenAPI.summary_builder.call(example)
2121
tags = metadata[:tags] || RSpec::OpenAPI.tags_builder.call(example)
2222
formats = metadata[:formats] || RSpec::OpenAPI.formats_builder.curry.call(example)
@@ -55,6 +55,26 @@ def request_response(context)
5555
[context.request, context.response]
5656
end
5757

58+
private
59+
60+
def merge_openapi_metadata(metadata)
61+
collect_openapi_metadata(metadata).reduce({}, &:merge)
62+
end
63+
64+
def collect_openapi_metadata(metadata)
65+
[].tap do |result|
66+
current = metadata
67+
68+
while current
69+
[current[:example_group], current].each do |meta|
70+
result.unshift(meta[:openapi]) if meta&.dig(:openapi)
71+
end
72+
73+
current = current[:parent_example_group]
74+
end
75+
end
76+
end
77+
5878
# @param [ActionDispatch::Request] request
5979
def find_rails_route(request, app: Rails.application, path_prefix: '')
6080
app.routes.router.recognize(request) do |route, _parameters|

spec/apps/hanami/doc/openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@
496496
},
497497
"/tables": {
498498
"get": {
499-
"summary": "index",
499+
"summary": "Get a list of tables",
500500
"tags": [
501501
"Table"
502502
],

spec/apps/hanami/doc/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ paths:
306306
message: not found
307307
"/tables":
308308
get:
309-
summary: index
309+
summary: Get a list of tables
310310
tags:
311311
- Table
312312
parameters:
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)