Skip to content

Commit 75e3b31

Browse files
committed
Fix reference creation when two types are provided
Previously the type from param definition would be used to documentation even if we try to override it in the `documentation` hash. It caused Swagger UI to render, for example, "Unknown Type: Color" instead of using the provided entity. It terms of code, before this change we would return something like this: { color: { type: 'Color' } instead of: { color: { '$ref' => 'ColorEntity' } if Color is not inherited from Grape::Entity Having different types can be useful when two different classes are used for parsing and presenting an attribute. E.g. the input format for Base64 File can be represented as a Hash (content, content_type and filename), whereas the type after parsing can be ActionDispatch::Http::UploadedFile. In the current situation the solution to this problem is to put coercion methods to the same entity and specify `type` only for the param without using `documentation`.
1 parent 445d2d4 commit 75e3b31

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
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+
* [#756](https://github.com/ruby-grape/grape-swagger/pull/756): Fix reference creation when custom type for documentation is provided - [@bikolya](https://github.com/bikolya).
1010

1111
### 0.33.0 (June 21, 2019)
1212

lib/grape-swagger/endpoint.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ def params_object(route, options, path)
178178
parameters = partition_params(route, options).map do |param, value|
179179
value = { required: false }.merge(value) if value.is_a?(Hash)
180180
_, value = default_type([[param, value]]).first if value == ''
181-
if value[:type]
182-
expose_params(value[:type])
183-
elsif value[:documentation]
181+
if value.dig(:documentation, :type)
184182
expose_params(value[:documentation][:type])
183+
elsif value[:type]
184+
expose_params(value[:type])
185185
end
186186
GrapeSwagger::DocMethods::ParseParams.call(param, value, path, route, @definitions)
187187
end

spec/swagger_v2/endpoint_versioned_path_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@
5252
expect(subject.first['/v1/item'][:get][:tags]).to eq ['special-item']
5353
end
5454
end
55+
56+
context 'when parameter with a custom type is specified' do
57+
let(:item) do
58+
Class.new(Grape::API) do
59+
Color = Struct.new(:value) do
60+
def self.parse(value)
61+
new(value: value)
62+
end
63+
end
64+
65+
class ColorEntity < Grape::Entity
66+
expose :value
67+
end
68+
69+
version 'v1', using: :path
70+
71+
resource :item do
72+
params do
73+
requires :root, type: Hash do
74+
optional :color, type: Color, documentation: { type: ColorEntity }
75+
end
76+
end
77+
post '/'
78+
end
79+
end
80+
end
81+
82+
it 'creates a reference to the model instead of using the non-existent type' do
83+
color = subject.dig(1, 'postV1Item', :properties, :root, :properties, :color)
84+
expect(color).not_to eq(type: 'ColorEntity')
85+
expect(color).to eq('$ref' => '#/definitions/ColorEntity')
86+
end
87+
end
5588
end
5689

5790
context 'when mounting an API more than once', if: GrapeVersion.satisfy?('>= 1.2.0') do

0 commit comments

Comments
 (0)