_build_response crashes with ValueError on non-IANA HTTP status codes (520, 561, etc.)
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 52/100
Línea de trabajo
Revisa templates/endpoint_module.py.jinja y types.py.jinja, comenzando por la conversión de HTTPStatus mostrada en el issue. Compara las alternativas con el PR relacionado #1407 y confirma el manejo previsto de los códigos de respuesta no pertenecientes a IANA; se considera terminado cuando los endpoints generados ya no generan ValueError antes del manejo de la respuesta y el comportamiento de códigos de estado elegido está cubierto.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Describe the bug
The generated _build_response function unconditionally calls HTTPStatus(response.status_code) to populate Response.status_code. Python's http.HTTPStatus is a closed IntEnum covering only IANA-registered codes — passing anything else raises ValueError: <n> is not a valid HTTPStatus.
This means any endpoint generated by openapi-python-client crashes whenever the network returns a non-IANA code, including:
- Cloudflare 520–527 (origin errors — extremely common when there's an upstream hiccup)
- AWS WAF 561
- nginx 499 (client closed request)
- Any proxy / CDN / origin that emits a custom code
The crash hits before any response handler runs, so even users who set raise_on_unexpected_status=True get a ValueError rather than an UnexpectedStatus exception they can catch.
Reproduction
from http import HTTPStatus
HTTPStatus(520)
# ValueError: 520 is not a valid HTTPStatus
Any generated endpoint module reproduces it:
# templates/endpoint_module.py.jinja, line 97
def _build_response(*, client, response):
return Response(
status_code=HTTPStatus(response.status_code), # <-- crashes here on 520
...
)
An httpx response with a 520 status from a Cloudflare-fronted origin will trip this on every call. No OpenAPI spec change is required — the bug is in the template, not the spec.
Expected behavior
_build_response should tolerate non-IANA codes — either by widening Response.status_code to HTTPStatus | int and falling back to the raw int, or by raising UnexpectedStatus with the raw code. Either is better than ValueError.
Desktop
- OS: macOS 15
- Python Version: 3.12
- openapi-python-client version: 0.28.4 (latest at time of filing — bug present from at least 0.20.x onward)
Related
- PR #1407 ("Support non standard http codes") proposes an opt-in
allow_int_response_codesflag. Filing this issue so the bug has a permanent searchable record and so the safer behavior can be considered as the default (since the current behavior crashes the generated client on transient upstream errors users can't influence).
Workaround
For others hitting this: override the endpoint_module.py.jinja and types.py.jinja templates via --custom-template-path, replacing the HTTPStatus(...) call with a try/except that falls back to the raw int.
- Lenguaje dominante
- Python
- Estrellas
- 2k
- Forks
- 293
- Merge medio
- 34 min
- PR fusionados (30 d)
- 1
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de openapi-generators/openapi-python-client
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
-
Dificultad 3/5 1-2 días Aptitud para principiantes 72/100
openapi-generators/openapi-python-client#1451 · 1 comentario ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 64/100
openapi-generators/openapi-python-client#1435 · 1 comentario ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
Todos los issues de openapi-generators/openapi-python-client
Issues similares
-
fix: inaccuracy ⚠️
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
uabrc/uabrc.github.io#1255 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
ethereum-optimism/factory#64 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 90/100
duckdb/duckdb-python#627 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Add link for tutorial Abiertodocumentation
Dificultad 1/5 Menos de una hora Aptitud para principiantes 78/100
Qiskit/qiskit-addon-sqd#376 ·