From 99e20548df3c02bfe47b7c697ba9f2edefcb8acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Lu=CC=88dke?= Date: Tue, 24 Jun 2025 14:20:26 +0200 Subject: [PATCH] Improve content type mismatch error message --- lib/openapi_first/router/find_response.rb | 22 ++++++++++++-------- spec/middlewares/response_validation_spec.rb | 2 +- spec/router/find_response_spec.rb | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/openapi_first/router/find_response.rb b/lib/openapi_first/router/find_response.rb index d745c2cb..d66dc662 100644 --- a/lib/openapi_first/router/find_response.rb +++ b/lib/openapi_first/router/find_response.rb @@ -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, diff --git a/spec/middlewares/response_validation_spec.rb b/spec/middlewares/response_validation_spec.rb index fa1cc2a4..532f7f45 100644 --- a/spec/middlewares/response_validation_spec.rb +++ b/spec/middlewares/response_validation_spec.rb @@ -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 diff --git a/spec/router/find_response_spec.rb b/spec/router/find_response_spec.rb index 5104e16e..5f1351b4 100644 --- a/spec/router/find_response_spec.rb +++ b/spec/router/find_response_spec.rb @@ -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:)