Skip to content

Commit 46e112c

Browse files
authored
Fix type/format of array[object] fields (#841)
* Add reproduction spec * Don't overwrite items field if it's already there Also add even more levels of nesting in spec to ensure things still work as expected. * Update CHANGELOG
1 parent 86b367b commit 46e112c

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

CHANGELOG.md

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

77
#### Fixes
88

9+
* [#841](https://github.com/ruby-grape/grape-swagger/pull/839): Fixes `type` and `format` values for object fields nested in an array ([#832](https://github.com/ruby-grape/grape-swagger/issue/832)) - [@magni-](https://github.com/magni-)
910
* #[#839](https://github.com/ruby-grape/grape-swagger/pull/839): Fixes documentation of `false` or `nil` default parameter values - [@magni-](https://github.com/magni-)
1011
* Your contribution here.
1112

lib/grape-swagger/doc_methods/format_data.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def add_braces(parameter, related_parameters)
3535

3636
def add_array(parameter, related_parameters)
3737
related_parameters.each do |p|
38+
next if p.key?(:items)
39+
3840
p_type = p[:type] == 'array' ? 'string' : p[:type]
3941
p[:items] = { type: p_type, format: p[:format], enum: p[:enum], is_array: p[:is_array] }
4042
p[:items].delete_if { |_k, v| v.nil? }
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe '#832 array of objects with nested Float/BigDecimal fields' do
6+
let(:app) do
7+
Class.new(Grape::API) do
8+
resource :issue_832 do
9+
params do
10+
requires :array_param, type: Array do
11+
requires :float_param, type: Float
12+
requires :big_decimal_param, type: BigDecimal
13+
requires :object_param, type: Hash do
14+
requires :float_param, type: Float
15+
requires :big_decimal_param, type: BigDecimal
16+
requires :object_param, type: Hash do
17+
requires :float_param, type: Float
18+
requires :big_decimal_param, type: BigDecimal
19+
requires :array_param, type: Array do
20+
requires :integer_param, type: Integer
21+
end
22+
end
23+
end
24+
end
25+
end
26+
post do
27+
{ message: 'hello world' }
28+
end
29+
end
30+
31+
add_swagger_documentation
32+
end
33+
end
34+
let(:parameters) { subject['paths']['/issue_832']['post']['parameters'] }
35+
36+
subject do
37+
get '/swagger_doc'
38+
JSON.parse(last_response.body)
39+
end
40+
41+
specify do
42+
expect(parameters).to eql(
43+
[
44+
{
45+
'in' => 'formData',
46+
'name' => 'array_param[float_param]',
47+
'type' => 'array',
48+
'required' => true,
49+
'items' => {
50+
'type' => 'number',
51+
'format' => 'float'
52+
}
53+
}, {
54+
'in' => 'formData',
55+
'name' => 'array_param[big_decimal_param]',
56+
'type' => 'array',
57+
'required' => true,
58+
'items' => {
59+
'type' => 'number',
60+
'format' => 'double'
61+
}
62+
}, {
63+
'in' => 'formData',
64+
'name' => 'array_param[object_param][float_param]',
65+
'type' => 'array',
66+
'required' => true,
67+
'items' => {
68+
'type' => 'number',
69+
'format' => 'float'
70+
}
71+
}, {
72+
'in' => 'formData',
73+
'name' => 'array_param[object_param][big_decimal_param]',
74+
'type' => 'array',
75+
'required' => true,
76+
'items' => {
77+
'type' => 'number',
78+
'format' => 'double'
79+
}
80+
}, {
81+
'in' => 'formData',
82+
'name' => 'array_param[object_param][object_param][float_param]',
83+
'type' => 'array',
84+
'required' => true,
85+
'items' => {
86+
'type' => 'number',
87+
'format' => 'float'
88+
}
89+
}, {
90+
'in' => 'formData',
91+
'name' => 'array_param[object_param][object_param][big_decimal_param]',
92+
'type' => 'array',
93+
'required' => true,
94+
'items' => {
95+
'type' => 'number',
96+
'format' => 'double'
97+
}
98+
}, {
99+
'in' => 'formData',
100+
'name' => 'array_param[object_param][object_param][array_param][integer_param]',
101+
'type' => 'array',
102+
'required' => true,
103+
'items' => {
104+
'type' => 'integer',
105+
'format' => 'int32'
106+
}
107+
}
108+
]
109+
)
110+
end
111+
end

0 commit comments

Comments
 (0)