Add strict_response_content_type option to ResponseValidation middleware#478
Open
whoward wants to merge 1 commit into
Open
Add strict_response_content_type option to ResponseValidation middleware#478whoward wants to merge 1 commit into
whoward wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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