Skip to content

Commit 4f0df3f

Browse files
authored
Merge pull request #377 from johannesluedke/improve-content_type-mismatch
Improve content type mismatch error message
2 parents 4f372d3 + 99e2054 commit 4f0df3f

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

lib/openapi_first/router/find_response.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@ def self.call(responses, status, content_type, request_method:, path:)
1616
return Match.new(error: Failure.new(:response_not_found, message:), response: nil)
1717
end
1818
response = FindContent.call(contents, content_type)
19-
if response.nil?
20-
message = "#{content_error(content_type, request_method:,
21-
path:)} Content-Type should be #{contents.keys.join(' or ')}."
22-
return Match.new(error: Failure.new(:response_not_found, message:), response: nil)
23-
end
19+
return response_not_found(content_type:, contents:, request_method:, path:) unless response
2420

2521
Match.new(response:, error: nil)
2622
end
2723

28-
def self.content_error(content_type, request_method:, path:)
29-
return 'Response Content-Type must not be empty.' if content_type.nil? || content_type.empty?
30-
31-
"Response Content-Type #{content_type} is not defined for #{request_method} #{path}."
24+
def self.response_not_found(content_type:, contents:, request_method:, path:)
25+
empty_content = content_type.nil? || content_type.empty?
26+
message =
27+
"Content-Type should be #{contents.keys.join(' or ')}, " \
28+
"but was #{empty_content ? 'empty' : content_type} for " \
29+
"#{request_method.upcase} #{path}"
30+
31+
Match.new(
32+
error: Failure.new(:response_not_found, message:),
33+
response: nil
34+
)
3235
end
36+
private_class_method :response_not_found
3337

3438
def self.find_status(responses, status)
3539
# According to OAS status has to be a string,

spec/middlewares/response_validation_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
end
5252

5353
it 'returns an error' do
54-
msg = 'Response Content-Type must not be empty. Content-Type should be application/json.'
54+
msg = 'Content-Type should be application/json, but was empty for GET /pets'
5555
expect do
5656
get '/pets'
5757
end.to raise_error OpenapiFirst::ResponseNotFoundError, msg

spec/router/find_response_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def find(responses, status, content_type)
8282
double(status: '200', content_type: 'application/text'),
8383
double(status: '200', content_type: 'application/xml')
8484
]
85-
message = 'Response Content-Type application/json is not defined for GET /stations. Content-Type should be application/text or application/xml.'
85+
message = 'Content-Type should be application/text or application/xml, but was application/json for GET /stations'
8686
expect(find(responses, 200, 'application/json')).to have_attributes(
8787
response: nil,
8888
error: have_attributes(type: :response_not_found, message:)

0 commit comments

Comments
 (0)