Skip to content

Commit 3189dfa

Browse files
blakenumbata
authored andcommitted
Initial support for openapi3 components/schemas
1 parent 94a3fee commit 3189dfa

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

lib/grape-swagger/openapi_3/doc_methods.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def setup(options)
5555

5656
output[:tags] = tags unless tags.empty? || paths.blank?
5757
output[:paths] = paths unless paths.blank?
58-
output[:definitions] = definitions unless definitions.blank?
58+
unless definitions.blank?
59+
output[:components] = {}
60+
output[:components][:schemas] = definitions
61+
end
5962

6063
output
6164
end

lib/grape-swagger/openapi_3/doc_methods/parse_params.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def document_array_param(value_type, definitions)
7474

7575
array_items = {}
7676
if definitions[value_type[:data_type]]
77-
array_items['$ref'] = "#/definitions/#{@parsed_param[:type]}"
77+
array_items['$ref'] = "#/components/schemas/#{@parsed_param[:type]}"
7878
else
7979
array_items[:type] = type || @parsed_param[:schema][:type] == 'array' ? 'string' : @parsed_param[:schema][:type]
8080
end

lib/grape-swagger/openapi_3/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def tag_object(route, path)
301301

302302
def build_reference(route, value, response_model)
303303
# TODO: proof that the definition exist, if model isn't specified
304-
reference = { '$ref' => "#/definitions/#{response_model}" }
304+
reference = { '$ref' => "#/components/schemas/#{response_model}" }
305305
route.options[:is_array] && value[:code] < 300 ? { type: 'array', items: reference } : reference
306306
end
307307

spec/openapi_3/api_openapi_3_response_spec.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,23 @@ def app
5353
'description' => 'This returns something',
5454
'content' => {
5555
'application/json' => {
56-
'schema' => { '$ref' => '#/definitions/UseItemResponseAsType' }
56+
'schema' => { '$ref' => '#/components/schemas/UseItemResponseAsType' }
5757
}
5858
}
5959
},
6060
'400' => {
6161
'description' => 'NotFound',
6262
'content' => {
6363
'application/json' => {
64-
'schema' => { '$ref' => '#/definitions/ApiError' }
64+
'schema' => { '$ref' => '#/components/schemas/ApiError' }
6565
}
6666
}
6767
}
6868
},
6969
'tags' => ['nested_type'],
7070
'operationId' => 'getNestedType'
7171
)
72-
expect(subject['definitions']).to eql(swagger_nested_type)
72+
expect(subject['components']['schemas']).to eql(swagger_nested_type)
7373
end
7474
end
7575

@@ -87,23 +87,23 @@ def app
8787
'description' => 'This returns something',
8888
'content' => {
8989
'application/json' => {
90-
'schema' => { '$ref' => '#/definitions/UseResponse' }
90+
'schema' => { '$ref' => '#/components/schemas/UseResponse' }
9191
}
9292
}
9393
},
9494
'400' => {
9595
'description' => 'NotFound',
9696
'content' => {
9797
'application/json' => {
98-
'schema' => { '$ref' => '#/definitions/ApiError' }
98+
'schema' => { '$ref' => '#/components/schemas/ApiError' }
9999
}
100100
}
101101
}
102102
},
103103
'tags' => ['entity_response'],
104104
'operationId' => 'getEntityResponse'
105105
)
106-
expect(subject['definitions']).to eql(swagger_entity_as_response_object)
106+
expect(subject['components']['schemas']).to eql(swagger_entity_as_response_object)
107107
end
108108
end
109109

@@ -114,7 +114,6 @@ def app
114114
end
115115

116116
specify do
117-
fail("TODO: Fix")
118117
expect(subject['paths']['/params_given']['post']).to eql(
119118
'description' => 'This returns something',
120119
'requestBody' => {
@@ -140,15 +139,15 @@ def app
140139
'description' => 'NotFound',
141140
'content' => {
142141
'application/json' => {
143-
'schema' => { '$ref' => '#/definitions/ApiError' }
142+
'schema' => { '$ref' => '#/components/schemas/ApiError' }
144143
}
145144
}
146145
}
147146
},
148147
'tags' => ['params_given'],
149148
'operationId' => 'postParamsGiven'
150149
)
151-
expect(subject['definitions']).to eql(swagger_params_as_response_object)
150+
expect(subject['components']['schemas']).to eql(swagger_params_as_response_object)
152151
end
153152
end
154153
end

0 commit comments

Comments
 (0)