Skip to content

Commit de6f094

Browse files
authored
Add extension properties to schema for body params (#918)
1 parent cb92e17 commit de6f094

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Fixes
88

9-
* Your contribution here.
9+
* [#918](https://github.com/ruby-grape/grape-swagger/pull/918): Fix params extension does not work when param_type is body - [@numbata](https://github.com/numbata)
1010

1111
### 2.0.1 (Januar 2, 2024)
1212

lib/grape-swagger/doc_methods/move_params.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def build_properties(params)
8686
else
8787
document_as_property(param)
8888
end
89+
add_extension_properties(properties[name], param)
8990

9091
required << name if deletable?(param) && param[:required]
9192
end
@@ -102,6 +103,12 @@ def document_as_array(param)
102103
end
103104
end
104105

106+
def add_extension_properties(definition, values)
107+
values.each do |key, value|
108+
definition[key] = value if key.start_with?('x-')
109+
end
110+
end
111+
105112
def document_as_property(param)
106113
property_keys.each_with_object({}) do |x, memo|
107114
next unless param.key?(x)

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def parse_array_item(definitions, type, value_type)
108108
set_additional_properties, additional_properties = parse_additional_properties(definitions, value_type)
109109
array_items[:additionalProperties] = additional_properties if set_additional_properties
110110

111+
if value_type.key?(:items)
112+
GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(value_type[:items], array_items)
113+
end
114+
111115
array_items
112116
end
113117

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe '#901 params extension does not work when param_type is body' do
6+
let(:app) do
7+
Class.new(Grape::API) do
8+
namespace :issue_901 do
9+
params do
10+
requires :user_id, type: Integer, documentation: { type: 'integer', param_type: 'body', x: { nullable: true } }
11+
requires :friend_ids, type: [Integer], documentation: { type: 'integer', is_array: true, param_type: 'body', x: { type: 'array' }, items: { x: { type: 'item' } } }
12+
requires :address, type: Hash, documentation: { type: 'object', param_type: 'body', x: { type: 'address' } } do
13+
requires :city_id, type: Integer, documentation: { type: 'integer', x: { type: 'city' } }
14+
end
15+
end
16+
post do
17+
present params
18+
end
19+
end
20+
21+
add_swagger_documentation format: :json
22+
end
23+
end
24+
25+
subject do
26+
get '/swagger_doc'
27+
JSON.parse(last_response.body)
28+
end
29+
30+
let(:definition) { subject['definitions']['postIssue901'] }
31+
32+
specify do
33+
expect(definition['properties']).to match(
34+
'user_id' => hash_including('type' => 'integer', 'x-nullable' => true),
35+
'address' => hash_including(
36+
'type' => 'object',
37+
'x-type' => 'address',
38+
'properties' => {
39+
'city_id' => hash_including('type' => 'integer', 'x-type' => 'city')
40+
}
41+
),
42+
'friend_ids' => hash_including(
43+
'type' => 'array',
44+
'x-type' => 'array',
45+
'items' => hash_including('type' => 'integer', 'x-type' => 'item')
46+
)
47+
)
48+
end
49+
end

0 commit comments

Comments
 (0)