Add strict_response_content_type option to ResponseValidation middleware - #478
Conversation
geemus
left a comment
There was a problem hiding this comment.
Overall looks good to me, though it looks like our test coverage dropped below a threshold (you were unlucky in your timing maybe). That said, it would probably be good to unit test the find_response_object_for_status part as it is a bit involved. Could you look at fleshing that out a bit more (and if coverage is still insufficient, we can discuss other options/tweaks).
Thanks and apologies for my delay in reviewing.
0d5391b to
0f5e144
Compare
Committee silently skips response body schema validation when the response Content-Type doesn't match any key declared in the OpenAPI spec's content map for that operation. This means a server returning application/vnd.api+json against a spec that only declares application/json passes validation without error. The new opt-in flag strict_response_content_type: false (default false for backwards compatibility) raises Committee::InvalidResponse in this case. The guard is skipped for responses with no content map at all (e.g. bare 204s), which continue to pass silently. The check is implemented as a pre-guard in OperationWrapper#validate_response_params before handing off to openapi_parser's validate_response_body, since openapi_parser has no hook to express this policy without modifying the gem or repurposing the existing strict: flag. Status code lookup replicates openapi_parser's wildcard matching (4XX, default) via a private find_response_object_for_status helper. Content-Type params (e.g. charset=utf-8) are stripped via Rack::MediaType.type before matching, consistent with how validate_post_request_params handles request body content types.
0f5e144 to
5dbafa4
Compare
|
@geemus no worries! I believe I've fixed the coverage issue and have added unit tests covering |
|
LGTM, thanks! |
Committee silently skips response body schema validation when the response
Content-Typedoesn't match any key declared in the OpenAPI spec's content map for that operation. This means a server returningapplication/vnd.api+jsonagainst a spec that only declaresapplication/jsonpasses validation without error. The new opt-in flagstrict_response_content_type: false(defaultfalsefor backwards compatibility) raisesCommittee::InvalidResponsein this case. The guard is skipped for responses with no content map at all (e.g. bare 204s), which continue to pass silently.The check is implemented as a pre-guard in
OperationWrapper#validate_response_paramsbefore handing off toopenapi_parser'svalidate_response_body, sinceopenapi_parserhas no hook to express this policy without modifying the gem or repurposing the existingstrict:flag. Status code lookup replicatesopenapi_parser's wildcard matching (4XX, default) via a privatefind_response_object_for_statushelper.Content-Typeparams (e.g. charset=utf-8) are stripped viaRack::MediaType.typebefore matching, consistent with howvalidate_post_request_paramshandles request body content types.This work implements #477