Skip to content

Commit

Permalink
Do not raise error when body is empty and nil content type
Browse files Browse the repository at this point in the history
Empty response bodies (204 responses for instance) should not have a
content type and therefore an error should not be raised in strict mode
when the response body is empty and the content type is nil as this
behavior is expected.
  • Loading branch information
Tristan O'Neil authored and tristanoneil committed Mar 30, 2022
1 parent bed7810 commit 69c8c9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/openapi_parser/schemas/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def validate(response_body, response_validate_options)

media_type = select_media_type(response_body.content_type)
unless media_type
raise ::OpenAPIParser::NotExistContentTypeDefinition, object_reference if response_validate_options.strict
if response_validate_options.strict && response_body_not_blank(response_body)
raise ::OpenAPIParser::NotExistContentTypeDefinition, object_reference
end

return nil
end
Expand All @@ -39,6 +41,11 @@ def select_media_type(content_type)

private

# @param [OpenAPIParser::RequestOperation::ValidatableResponseBody]
def response_body_not_blank(response_body)
!(response_body.response_data.nil? || response_body.response_data.empty?)
end

# @param [Hash] response_headers
def validate_header(response_headers)
return unless headers
Expand Down
16 changes: 15 additions & 1 deletion spec/openapi_parser/request_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
let(:content_type) { nil }
let(:data) { { 'string' => 1 } }

it { expect(subject).to eq nil }
it { expect(subject).to eq true }
end

context 'with header' do
Expand Down Expand Up @@ -159,6 +159,7 @@

context 'not exist content type' do
let(:content_type) { 'application/xml' }
let(:data) { '<something></something>' }

it do
expect { subject }.to raise_error do |e|
Expand All @@ -167,6 +168,18 @@
end
end
end

context 'with nil content type when the response body is blank' do
let(:status_code) { 204 }
let(:content_type) { nil }
let(:data) { '' }
let(:http_method) { :get }

it do
expect { subject }.to_not raise_error
expect(subject).to eq true
end
end
end

context 'default parameter' do
Expand All @@ -193,6 +206,7 @@

context 'not exist content type' do
let(:content_type) { 'application/xml' }
let(:data) { '<something></something>' }

it do
expect { subject }.to raise_error do |e|
Expand Down

0 comments on commit 69c8c9a

Please sign in to comment.