Skip to content

Commit 383db67

Browse files
committed
Respect primitve mapping on type and format attributs of 1.2 swagger spec: https://github.com/swagger-api/swagger-spec/blob/master/versions/1.2.md#431-primitives
1 parent a0940ce commit 383db67

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [#225](https://github.com/tim-vandecasteele/grape-swagger/pull/225): Fixed `param_type` to have it read from parameter's documentation hash - [@zsxking](https://github.com/zsxking).
1818
* [#235](https://github.com/tim-vandecasteele/grape-swagger/pull/235): Fixed nested entity names in parameters and as `$ref` in models - [@frodrigo](https://github.com/frodrigo).
1919
* [#206](https://github.com/tim-vandecasteele/grape-swagger/pull/206): Fixed 'is_array' in the return entity being ignored - [@igormoochnick](https://github.com/igormoochnick).
20+
* [#266](https://github.com/tim-vandecasteele/grape-swagger/pull/266): Respect primitve mapping on type and format attributs of 1.2 swagger spec - [@frodrigo](https://github.com/frodrigo).
2021

2122
### 0.10.1 (March 11, 2015)
2223

lib/grape-swagger/doc_methods.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
module GrapeSwagger
22
module DocMethods
3+
PRIMITIVE_MAPPINGS = {
4+
'integer' => %w(integer int32),
5+
'long' => %w(integer int64),
6+
'float' => %w(number float),
7+
'double' => %w(number double),
8+
'byte' => %w(string byte),
9+
'date' => %w(string date),
10+
'dateTime' => %w(string date-time)
11+
}
12+
313
def name
414
@@class_name
515
end
@@ -86,8 +96,10 @@ def parse_params(params, path, method)
8696
allowMultiple: is_array
8797
}
8898

89-
parsed_params[:format] = 'int32' if data_type == 'integer'
90-
parsed_params[:format] = 'int64' if data_type == 'long'
99+
if PRIMITIVE_MAPPINGS.key?(data_type)
100+
parsed_params[:type], parsed_params[:format] = PRIMITIVE_MAPPINGS[data_type]
101+
end
102+
91103
parsed_params[:items] = items if items.present?
92104

93105
parsed_params[:defaultValue] = example if example
@@ -208,6 +220,10 @@ def parse_entity_models(models)
208220
p[:enum] = select_values
209221
end
210222

223+
if PRIMITIVE_MAPPINGS.key?(p['type'])
224+
p['type'], p['format'] = PRIMITIVE_MAPPINGS[p['type']]
225+
end
226+
211227
properties[property_name] = p
212228
end
213229

spec/api_global_models_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def app
7070
'id' => 'Some::CombinedThing',
7171
'properties' => {
7272
'text' => { 'type' => 'string', 'description' => 'Content of something.' },
73-
'created_at' => { 'type' => 'dateTime', 'description' => 'Creation of something.' }
73+
'created_at' => { 'type' => 'string', 'format' => 'date-time', 'description' => 'Creation of something.' }
7474
}
7575
})
7676
end

spec/api_models_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def app
258258
'id' => 'EnumValues',
259259
'properties' => {
260260
'gender' => { 'type' => 'string', 'description' => 'Content of something.', 'enum' => %w(Male Female) },
261-
'number' => { 'type' => 'integer', 'description' => 'Content of something.', 'enum' => [1, 2] }
261+
'number' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'Content of something.', 'enum' => [1, 2] }
262262
}
263263
)
264264

spec/float_api_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def app
2424

2525
it 'converts float types' do
2626
expect(subject).to eq [
27-
{ 'paramType' => 'form', 'name' => 'a_float', 'description' => nil, 'type' => 'float', 'required' => true, 'allowMultiple' => false }
27+
{ 'paramType' => 'form', 'name' => 'a_float', 'description' => nil, 'type' => 'number', 'format' => 'float', 'required' => true, 'allowMultiple' => false }
2828
]
2929
end
3030
end

spec/param_values_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def first_parameter_info(request)
123123

124124
it 'has float range values as string' do
125125
expect(range_float).to eq [
126-
{ 'paramType' => 'form', 'name' => 'float', 'description' => nil, 'type' => 'float', 'required' => true, 'allowMultiple' => false }
126+
{ 'paramType' => 'form', 'name' => 'float', 'description' => nil, 'type' => 'number', 'format' => 'float', 'required' => true, 'allowMultiple' => false }
127127
]
128128
end
129129
end

0 commit comments

Comments
 (0)