Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/openapi_first/router/find_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ def self.call(responses, status, content_type, request_method:, path:)
return Match.new(error: Failure.new(:response_not_found, message:), response: nil)
end
response = FindContent.call(contents, content_type)
if response.nil?
message = "#{content_error(content_type, request_method:,
path:)} Content-Type should be #{contents.keys.join(' or ')}."
return Match.new(error: Failure.new(:response_not_found, message:), response: nil)
end
return response_not_found(content_type:, contents:, request_method:, path:) unless response

Match.new(response:, error: nil)
end

def self.content_error(content_type, request_method:, path:)
return 'Response Content-Type must not be empty.' if content_type.nil? || content_type.empty?

"Response Content-Type #{content_type} is not defined for #{request_method} #{path}."
def self.response_not_found(content_type:, contents:, request_method:, path:)
empty_content = content_type.nil? || content_type.empty?
message =
"Content-Type should be #{contents.keys.join(' or ')}, " \
"but was #{empty_content ? 'empty' : content_type} for " \
"#{request_method.upcase} #{path}"

Match.new(
error: Failure.new(:response_not_found, message:),
response: nil
)
end
private_class_method :response_not_found

def self.find_status(responses, status)
# According to OAS status has to be a string,
Expand Down
2 changes: 1 addition & 1 deletion spec/middlewares/response_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
end

it 'returns an error' do
msg = 'Response Content-Type must not be empty. Content-Type should be application/json.'
msg = 'Content-Type should be application/json, but was empty for GET /pets'
expect do
get '/pets'
end.to raise_error OpenapiFirst::ResponseNotFoundError, msg
Expand Down
2 changes: 1 addition & 1 deletion spec/router/find_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def find(responses, status, content_type)
double(status: '200', content_type: 'application/text'),
double(status: '200', content_type: 'application/xml')
]
message = 'Response Content-Type application/json is not defined for GET /stations. Content-Type should be application/text or application/xml.'
message = 'Content-Type should be application/text or application/xml, but was application/json for GET /stations'
expect(find(responses, 200, 'application/json')).to have_attributes(
response: nil,
error: have_attributes(type: :response_not_found, message:)
Expand Down