Skip to content

fix: handle non-IANA HTTP status codes without crashing - #1471

Open
adeev-mardia wants to merge 2 commits into
openapi-generators:mainfrom
adeev-mardia:fix/non-iana-status-codes
Open

fix: handle non-IANA HTTP status codes without crashing#1471
adeev-mardia wants to merge 2 commits into
openapi-generators:mainfrom
adeev-mardia:fix/non-iana-status-codes

Conversation

@adeev-mardia

Copy link
Copy Markdown

Problem

Generated clients crash with ValueError when the server returns a non-IANA HTTP status code such as:

  • 520 – Cloudflare "Web Server Returned an Unknown Error"
  • 561 – AWS WAF / Application Load Balancer unauthorized
  • 499 – nginx "Client Closed Request"

The crash happens inside _build_response in every generated endpoint module:

def _build_response(...):
    return Response(
        status_code=HTTPStatus(response.status_code),  # ValueError on 520, 561, 499
        ...
    )

http.HTTPStatus is a closed IntEnum — it only accepts IANA-registered codes. Any code outside that set raises ValueError.

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.jinja

Replace the bare HTTPStatus(response.status_code) call with a try/except that falls back to the raw integer:

# Before
status_code=HTTPStatus(response.status_code),

# After
try:
    status_code = HTTPStatus(response.status_code)
except ValueError:
    status_code = response.status_code

openapi_python_client/templates/types.py.jinja

Widen the Response.status_code type annotation to accept both known and unknown codes:

# Before
status_code: HTTPStatus

# After
status_code: HTTPStatus | int

Why this approach?

  • Zero breaking changeHTTPStatus is a subclass of int, so all existing code that compares response.status_code == 200 or response.status_code == HTTPStatus.OK continues to work without modification.
  • No new public API — unlike the opt-in flag proposed in Support non standard http codes #1407, this just makes the default path not crash.
  • Minimal diff — two template files, two targeted changes.

Testing

To reproduce the crash before this fix:

from http import HTTPStatus
HTTPStatus(520)  # ValueError: 520 is not a valid HTTPStatus

After the fix, _build_response returns a Response with status_code=520 (plain int) instead of raising.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_build_response crashes with ValueError on non-IANA HTTP status codes (520, 561, etc.)

1 participant