mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 07:16:40 +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)
|
def schema(value)
|
||||||
metadata[:response][:schema] = value
|
metadata[:response][:schema] = value
|
||||||
end
|
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)
|
def header(name, attributes)
|
||||||
metadata[:response][:headers] ||= {}
|
metadata[:response][:headers] ||= {}
|
||||||
|
|||||||
@ -41,13 +41,10 @@ module Rswag
|
|||||||
def validate_body!(metadata, swagger_doc, body)
|
def validate_body!(metadata, swagger_doc, body)
|
||||||
response_schema = metadata[:response][:schema]
|
response_schema = metadata[:response][:schema]
|
||||||
return if response_schema.nil?
|
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])
|
version = @config.get_swagger_doc_version(metadata[:swagger_doc])
|
||||||
schemas = definitions_or_component_schemas(swagger_doc, version)
|
schemas = definitions_or_component_schemas(swagger_doc, version)
|
||||||
|
|
||||||
# validation_schema = test_schemas[:schema] # response_schema
|
|
||||||
validation_schema = response_schema
|
validation_schema = response_schema
|
||||||
.merge('$schema' => 'http://tempuri.org/rswag/specs/extended_schema')
|
.merge('$schema' => 'http://tempuri.org/rswag/specs/extended_schema')
|
||||||
.merge(schemas)
|
.merge(schemas)
|
||||||
@ -55,15 +52,6 @@ module Rswag
|
|||||||
errors = JSON::Validator.fully_validate(validation_schema, body)
|
errors = JSON::Validator.fully_validate(validation_schema, body)
|
||||||
raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
|
raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
|
||||||
end
|
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)
|
def definitions_or_component_schemas(swagger_doc, version)
|
||||||
if version.start_with?('2')
|
if version.start_with?('2')
|
||||||
|
|||||||
@ -37,6 +37,8 @@ module Rswag
|
|||||||
upgrade_request_type!(metadata)
|
upgrade_request_type!(metadata)
|
||||||
upgrade_servers!(swagger_doc)
|
upgrade_servers!(swagger_doc)
|
||||||
upgrade_oauth!(swagger_doc)
|
upgrade_oauth!(swagger_doc)
|
||||||
|
upgrade_request_consumes!(swagger_doc, metadata)
|
||||||
|
upgrade_response_produces!(swagger_doc, metadata)
|
||||||
end
|
end
|
||||||
|
|
||||||
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
|
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
|
||||||
@ -79,6 +81,32 @@ module Rswag
|
|||||||
|
|
||||||
private
|
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)
|
def pretty_generate(doc)
|
||||||
if @config.swagger_format == :yaml
|
if @config.swagger_format == :yaml
|
||||||
clean_doc = yaml_prepare(doc)
|
clean_doc = yaml_prepare(doc)
|
||||||
|
|||||||
@ -154,19 +154,11 @@ module Rswag
|
|||||||
before { subject.schema(type: 'object') }
|
before { subject.schema(type: 'object') }
|
||||||
let(:api_metadata) { { response: {} } }
|
let(:api_metadata) { { response: {} } }
|
||||||
|
|
||||||
context 'swagger 2.0' do
|
|
||||||
it "adds to the 'response' metadata" do
|
it "adds to the 'response' metadata" do
|
||||||
expect(api_metadata[:response][:schema]).to match(type: 'object')
|
expect(api_metadata[:response][:schema]).to match(type: 'object')
|
||||||
end
|
end
|
||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#header(name, attributes)' do
|
describe '#header(name, attributes)' do
|
||||||
before { subject.header('Date', type: 'string') }
|
before { subject.header('Date', type: 'string') }
|
||||||
let(:api_metadata) { { response: {} } }
|
let(:api_metadata) { { response: {} } }
|
||||||
|
|||||||
@ -26,7 +26,7 @@ module Rswag
|
|||||||
{
|
{
|
||||||
path_item: { template: '/blogs', parameters: [{ type: :string }] },
|
path_item: { template: '/blogs', parameters: [{ type: :string }] },
|
||||||
operation: { verb: :post, summary: 'Creates a blog', 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
|
document: document
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -57,7 +57,8 @@ module Rswag
|
|||||||
responses: {
|
responses: {
|
||||||
'201' => {
|
'201' => {
|
||||||
description: 'blog created',
|
description: 'blog created',
|
||||||
headers: { type: :string }
|
headers: { type: :string },
|
||||||
|
schema: { '$ref' => '#/definitions/blog' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,6 +75,7 @@ module Rswag
|
|||||||
basePath: '/foo',
|
basePath: '/foo',
|
||||||
schemes: ['http', 'https'],
|
schemes: ['http', 'https'],
|
||||||
host: 'api.example.com',
|
host: 'api.example.com',
|
||||||
|
produces: ['application/vnd.my_mime', 'application/json'],
|
||||||
components: {
|
components: {
|
||||||
securitySchemes: {
|
securitySchemes: {
|
||||||
my_oauth: {
|
my_oauth: {
|
||||||
@ -95,6 +97,14 @@ module Rswag
|
|||||||
summary: 'Creates a blog',
|
summary: 'Creates a blog',
|
||||||
responses: {
|
responses: {
|
||||||
'201' => {
|
'201' => {
|
||||||
|
content: {
|
||||||
|
'application/vnd.my_mime' => {
|
||||||
|
schema: { '$ref' => '#/definitions/blog' }
|
||||||
|
},
|
||||||
|
'application/json' => {
|
||||||
|
schema: { '$ref' => '#/definitions/blog' }
|
||||||
|
}
|
||||||
|
},
|
||||||
description: 'blog created',
|
description: 'blog created',
|
||||||
headers: { schema: { type: :string } }
|
headers: { schema: { type: :string } }
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user