mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Add upgrades for consumes and produces in content with schemas
This commit is contained in:
parent
c739228c89
commit
405ccca494
@ -149,11 +149,6 @@ module Rswag
|
||||
def schema(value)
|
||||
metadata[:response][:schema] = value
|
||||
end
|
||||
## OA3
|
||||
# def schema(value, content_type: 'application/json')
|
||||
# content_hash = {content_type => {schema: value}}
|
||||
# metadata[:response][:content] = content_hash
|
||||
# end
|
||||
|
||||
def header(name, attributes)
|
||||
metadata[:response][:headers] ||= {}
|
||||
|
||||
@ -41,13 +41,10 @@ module Rswag
|
||||
def validate_body!(metadata, swagger_doc, body)
|
||||
response_schema = metadata[:response][:schema]
|
||||
return if response_schema.nil?
|
||||
## OA3
|
||||
# test_schemas = extract_schemas(metadata)
|
||||
# return if test_schemas.nil? || test_schemas.empty?
|
||||
|
||||
version = @config.get_swagger_doc_version(metadata[:swagger_doc])
|
||||
schemas = definitions_or_component_schemas(swagger_doc, version)
|
||||
|
||||
# validation_schema = test_schemas[:schema] # response_schema
|
||||
validation_schema = response_schema
|
||||
.merge('$schema' => 'http://tempuri.org/rswag/specs/extended_schema')
|
||||
.merge(schemas)
|
||||
@ -55,15 +52,6 @@ module Rswag
|
||||
errors = JSON::Validator.fully_validate(validation_schema, body)
|
||||
raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
|
||||
end
|
||||
## OA3
|
||||
# def extract_schemas(metadata)
|
||||
# metadata[:operation] = {produces: []} if metadata[:operation].nil?
|
||||
# produces = Array(metadata[:operation][:produces])
|
||||
|
||||
# producer_content = produces.first || 'application/json'
|
||||
# response_content = metadata[:response][:content] || {producer_content => {}}
|
||||
# response_content[producer_content]
|
||||
# end
|
||||
|
||||
def definitions_or_component_schemas(swagger_doc, version)
|
||||
if version.start_with?('2')
|
||||
|
||||
@ -37,6 +37,8 @@ module Rswag
|
||||
upgrade_request_type!(metadata)
|
||||
upgrade_servers!(swagger_doc)
|
||||
upgrade_oauth!(swagger_doc)
|
||||
upgrade_request_consumes!(swagger_doc, metadata)
|
||||
upgrade_response_produces!(swagger_doc, metadata)
|
||||
end
|
||||
|
||||
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
|
||||
@ -79,6 +81,32 @@ module Rswag
|
||||
|
||||
private
|
||||
|
||||
def upgrade_request_consumes!(swagger_doc, metadata)
|
||||
# Content-Type header
|
||||
mime_list = Array(metadata[:operation].delete(:consumes) || swagger_doc[:consumes])
|
||||
target_node = metadata[:response]
|
||||
upgrade_content!(mime_list, target_node)
|
||||
end
|
||||
|
||||
def upgrade_response_produces!(swagger_doc, metadata)
|
||||
# Accept header
|
||||
mime_list = Array(metadata[:operation].delete(:produces) || swagger_doc[:produces])
|
||||
target_node = metadata[:response]
|
||||
upgrade_content!(mime_list, target_node)
|
||||
metadata[:response].delete(:schema)
|
||||
end
|
||||
|
||||
def upgrade_content!(mime_list, target_node)
|
||||
target_node.merge!(content: {})
|
||||
schema = target_node[:schema]
|
||||
return if mime_list.empty?
|
||||
|
||||
mime_list.each do |mime_type|
|
||||
# TODO upgrade to have content-type specific schema
|
||||
target_node[:content][mime_type] = { schema: schema }
|
||||
end
|
||||
end
|
||||
|
||||
def pretty_generate(doc)
|
||||
if @config.swagger_format == :yaml
|
||||
clean_doc = yaml_prepare(doc)
|
||||
|
||||
@ -154,16 +154,8 @@ module Rswag
|
||||
before { subject.schema(type: 'object') }
|
||||
let(:api_metadata) { { response: {} } }
|
||||
|
||||
context 'swagger 2.0' do
|
||||
it "adds to the 'response' metadata" do
|
||||
expect(api_metadata[:response][:schema]).to match(type: 'object')
|
||||
end
|
||||
end
|
||||
|
||||
context 'openapi 3.0' do
|
||||
it "adds to the 'response' metadata" do
|
||||
expect(api_metadata[:response][:content]['application/json'][:schema]).to match(type: 'object')
|
||||
end
|
||||
it "adds to the 'response' metadata" do
|
||||
expect(api_metadata[:response][:schema]).to match(type: 'object')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -26,7 +26,7 @@ module Rswag
|
||||
{
|
||||
path_item: { template: '/blogs', parameters: [{ type: :string }] },
|
||||
operation: { verb: :post, summary: 'Creates a blog', parameters: [{ type: :string }] },
|
||||
response: { code: '201', description: 'blog created', headers: { type: :string } },
|
||||
response: { code: '201', description: 'blog created', headers: { type: :string }, schema: { '$ref' => '#/definitions/blog' } },
|
||||
document: document
|
||||
}
|
||||
end
|
||||
@ -57,7 +57,8 @@ module Rswag
|
||||
responses: {
|
||||
'201' => {
|
||||
description: 'blog created',
|
||||
headers: { type: :string }
|
||||
headers: { type: :string },
|
||||
schema: { '$ref' => '#/definitions/blog' }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -74,6 +75,7 @@ module Rswag
|
||||
basePath: '/foo',
|
||||
schemes: ['http', 'https'],
|
||||
host: 'api.example.com',
|
||||
produces: ['application/vnd.my_mime', 'application/json'],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
my_oauth: {
|
||||
@ -95,6 +97,14 @@ module Rswag
|
||||
summary: 'Creates a blog',
|
||||
responses: {
|
||||
'201' => {
|
||||
content: {
|
||||
'application/vnd.my_mime' => {
|
||||
schema: { '$ref' => '#/definitions/blog' }
|
||||
},
|
||||
'application/json' => {
|
||||
schema: { '$ref' => '#/definitions/blog' }
|
||||
}
|
||||
},
|
||||
description: 'blog created',
|
||||
headers: { schema: { type: :string } }
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user