fix: handle non-IANA HTTP status codes without crashing - #1471
Open
adeev-mardia wants to merge 2 commits into
Open
fix: handle non-IANA HTTP status codes without crashing#1471adeev-mardia wants to merge 2 commits into
adeev-mardia wants to merge 2 commits into
Conversation
Add try/except in _build_response so that non-IANA status codes (e.g. 520 Cloudflare, 561 AWS WAF, 499 nginx) fall back to raw int instead of raising ValueError from HTTPStatus(). Fixes openapi-generators#1442
Widen Response.status_code type from HTTPStatus to HTTPStatus | int so generated clients don't crash with ValueError on vendor-specific status codes like 520 (Cloudflare), 561 (AWS WAF), or 499 (nginx). Fixes openapi-generators#1442
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.
Problem
Generated clients crash with
ValueErrorwhen the server returns a non-IANA HTTP status code such as:The crash happens inside
_build_responsein every generated endpoint module:http.HTTPStatusis a closedIntEnum— it only accepts IANA-registered codes. Any code outside that set raisesValueError.This is a silent runtime failure: users see an unhandled exception even though their API call succeeded.
Fixes #1442.
Changes
openapi_python_client/templates/endpoint_module.py.jinjaReplace the bare
HTTPStatus(response.status_code)call with a try/except that falls back to the raw integer:openapi_python_client/templates/types.py.jinjaWiden the
Response.status_codetype annotation to accept both known and unknown codes:Why this approach?
HTTPStatusis a subclass ofint, so all existing code that comparesresponse.status_code == 200orresponse.status_code == HTTPStatus.OKcontinues to work without modification.Testing
To reproduce the crash before this fix:
After the fix,
_build_responsereturns aResponsewithstatus_code=520(plainint) instead of raising.