feat: Support responses with multiple media types in content
#1293
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.
This PR modifies
openapi-python-client
to recognize multiple media types in OpenAPI response objects and to differentially parse HTTP responses based on theirContent-Type
header values. (Today, it generates code for a single content media type of the ones it encounters in the OpenAPI document.) This is useful eg for modeling services where a load balancer can send a 429 with plain text but the service behind it might send one with a schematized JSON response type.Feedback is welcome, especially on the overall approach.
This PR adds unit tests and one end-to-end test, the latter of which includes sample generated code in the
end_to_end_tests/multiple-media-types-golden-record/
subdirectory.To maintain backward compatibility and minimize impact on existing callers, unexpected response content types raise the existing
UnexpectedStatus
error, rather than introducing a new error classUncovered diff lines:
openapi_python_client/parser/properties/protocol.py:71
: This is one of two properties added that wrap the getter functions for the type string and instance type string to allow them to be used in Jinja templates.openapi_python_client/parser/openapi.py:204
: This is a "belt-and-suspenders" check to avoid accessing an optional field if it is null. However, responses.py will not actually populate anyMediaType
instances with a nullprop
.