mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 07:16:40 +00:00
Fixes response_validator to handle 3.0 responses and validate against the schema.
JSON::Validator already handles anyOf oneOf schema definitions, so those can be passed in and validation errors are returned properly
This commit is contained in:
@@ -55,7 +55,15 @@ module Rswag
|
||||
def request_body(attributes)
|
||||
# can make this generic, and accept any incoming hash (like parameter method)
|
||||
attributes.compact!
|
||||
metadata[:operation][:requestBody] = attributes
|
||||
|
||||
if metadata[:operation][:requestBody].blank?
|
||||
metadata[:operation][:requestBody] = attributes
|
||||
elsif metadata[:operation][:requestBody] && metadata[:operation][:requestBody][:content]
|
||||
# merge in
|
||||
content_hash = metadata[:operation][:requestBody][:content]
|
||||
incoming_content_hash = attributes[:content]
|
||||
content_hash.merge!(incoming_content_hash) if incoming_content_hash
|
||||
end
|
||||
end
|
||||
|
||||
def request_body_json(schema:, required: true, description: nil, examples: nil)
|
||||
@@ -77,6 +85,18 @@ module Rswag
|
||||
end
|
||||
end
|
||||
|
||||
def request_body_text_plain(required: false, description: nil, examples: nil)
|
||||
content_hash = { 'test/plain' => { schema: {type: :string}, examples: examples }.compact! || {} }
|
||||
request_body(description: description, required: required, content: content_hash)
|
||||
end
|
||||
|
||||
# TODO: add examples to this like we can for json, might be large lift as many assumptions are made on content-type
|
||||
def request_body_xml(schema:,required: false, description: nil, examples: nil)
|
||||
passed_examples = Array(examples)
|
||||
content_hash = { 'application/xml' => { schema: schema, examples: examples }.compact! || {} }
|
||||
request_body(description: description, required: required, content: content_hash)
|
||||
end
|
||||
|
||||
def request_body_multipart(schema:, description: nil)
|
||||
content_hash = { 'multipart/form-data' => { schema: schema }}
|
||||
request_body(description: description, content: content_hash)
|
||||
|
||||
@@ -37,21 +37,27 @@ module Rswag
|
||||
raise UnexpectedResponse, "Expected response header #{name} to be present" if headers[name.to_s].nil?
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def validate_body!(metadata, swagger_doc, body)
|
||||
response_schema = metadata[:response][:schema]
|
||||
return if response_schema.nil?
|
||||
test_schemas = extract_schemas(metadata)
|
||||
return if test_schemas.nil?
|
||||
|
||||
components = swagger_doc[:components] || {}
|
||||
components_schemas = { components: { schemas: components[:schemas] } }
|
||||
|
||||
validation_schema = response_schema
|
||||
validation_schema = test_schemas[:schema] # response_schema
|
||||
.merge('$schema' => 'http://tempuri.org/rswag/specs/extended_schema')
|
||||
.merge(components_schemas)
|
||||
|
||||
errors = JSON::Validator.fully_validate(validation_schema, body)
|
||||
raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
|
||||
end
|
||||
|
||||
def extract_schemas(metadata)
|
||||
produces = Array(metadata[:operation][:produces])
|
||||
response_content = metadata[:response][:content] || {}
|
||||
producer_content = produces.first || 'application/json'
|
||||
response_content[producer_content]
|
||||
end
|
||||
end
|
||||
|
||||
class UnexpectedResponse < StandardError; end
|
||||
|
||||
Reference in New Issue
Block a user