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

オープン
#1,442 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
静か
技術スタック
python
領域
api, tooling

調査の方向性

templates/endpoint_module.py.jinja と types.py.jinja を確認し、issue に示されている HTTPStatus 変換から始めてください。関連する PR #1407 と代替案を比較し、IANA にないレスポンスコードの意図した扱いを確認してください。完了の条件は、生成されたエンドポイントがレスポンス処理の前に ValueError を発生させなくなり、選択したステータスコードの動作がカバーされていることです。

索引モデルが issue の本文から書いたものです。

説明

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_codes flag. 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.

主要言語
Python
スター
2k
フォーク
293
平均マージ
34分
マージ済み PR(30日)
1

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openapi-generators/openapi-python-client のほかの issue

openapi-generators/openapi-python-client の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。