Skip to content

Commit 635ecb3

Browse files
tristanoneilTristan O'Neil
and
Tristan O'Neil
authored
Correctly validate 204 responses (#133)
* Do not raise error when body is empty and nil content type 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. * Return true instead of nil when validation passes As mentioned in #101 it makes more sense to return true from this method when validation passes instead of nil. --------- Co-authored-by: Tristan O'Neil <tristan.oneil@calendly.com>
1 parent f72e3a0 commit 635ecb3

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/openapi_parser/schemas/response.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ def validate(response_body, response_validate_options)
2121

2222
media_type = select_media_type(response_body.content_type)
2323
unless media_type
24-
raise ::OpenAPIParser::NotExistContentTypeDefinition, object_reference if response_validate_options.strict
24+
if response_validate_options.strict && response_body_not_blank(response_body)
25+
raise ::OpenAPIParser::NotExistContentTypeDefinition, object_reference
26+
end
2527

26-
return nil
28+
return true
2729
end
2830

2931
options = ::OpenAPIParser::SchemaValidator::Options.new # response validator not support any options
@@ -39,6 +41,11 @@ def select_media_type(content_type)
3941

4042
private
4143

44+
# @param [OpenAPIParser::RequestOperation::ValidatableResponseBody]
45+
def response_body_not_blank(response_body)
46+
!(response_body.response_data.nil? || response_body.response_data.empty?)
47+
end
48+
4249
# @param [Hash] response_headers
4350
def validate_header(response_headers)
4451
return unless headers

spec/openapi_parser/request_operation_spec.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
let(:content_type) { nil }
6868
let(:data) { { 'string' => 1 } }
6969

70-
it { expect(subject).to eq nil }
70+
it { expect(subject).to eq true }
7171
end
7272

7373
context 'with header' do
@@ -155,6 +155,7 @@
155155

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

159160
it do
160161
expect { subject }.to raise_error do |e|
@@ -163,6 +164,18 @@
163164
end
164165
end
165166
end
167+
168+
context 'with nil content type when the response body is blank' do
169+
let(:status_code) { 204 }
170+
let(:content_type) { nil }
171+
let(:data) { '' }
172+
let(:http_method) { :get }
173+
174+
it do
175+
expect { subject }.to_not raise_error
176+
expect(subject).to eq true
177+
end
178+
end
166179
end
167180

168181
context 'default parameter' do
@@ -189,6 +202,7 @@
189202

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

193207
it do
194208
expect { subject }.to raise_error do |e|

0 commit comments

Comments
 (0)