openapi-generators / openapi-generators/openapi-python-client
Incorrect `const` property validation
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 2k
- フォーク
- 293
- 平均マージ
- 34分
- マージ済み PR(30日)
- 1
説明
Describe the bug
The const property validation introduced in #1024 is not correct when there are >= 2 non-none values.
In particular, the validation accepts only the first value
OpenAPI Spec File
--> Please focus on the Foo schema at the end
{
"openapi": "3.1.0",
"info": {
"title": "Test Service",
"version": "0.1.0"
},
"paths": {
"/test": {
"post": {
"summary": "Test",
"operationId": "test",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestModel"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
}
}
}
}
},
"components": {
"schemas": {
"TestModel": {
"properties": {
"foo": {
"$ref": "#/components/schemas/Foo"
}
},
"type": "object",
"required": [
"foo"
],
"title": "TestModel"
},
"Foo": {
"oneOf": [
{
"const": 1
},
{
"const": 2
}
],
"title": "FooType"
}
}
}
}
Desktop (please complete the following information):
- OS: all
- Python Version: all
- openapi-python-client version: the latest code, after #1024
Additional context
The above schema generates the file test-service-client/test_service_client/models/test_model.py as follows:
class TestModel:
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
d = src_dict.copy()
def _parse_foo(data: object) -> Union[Literal[1], Literal[2]]:
componentsschemas_foo_type_0 = cast(Literal[1], data)
if componentsschemas_foo_type_0 != 1:
raise ValueError(
f"/components/schemas/Foo_type_0 must match const 1, got '{componentsschemas_foo_type_0}'"
)
return componentsschemas_foo_type_0
# -----> ERROR HERE: the code below this line is never reached<------
componentsschemas_foo_type_1 = cast(Literal[2], data)
if componentsschemas_foo_type_1 != 2:
raise ValueError(
f"/components/schemas/Foo_type_1 must match const 2, got '{componentsschemas_foo_type_1}'"
)
return componentsschemas_foo_type_1
foo = _parse_foo(d.pop("foo"))
test_model = cls(
foo=foo,
)
test_model.additional_properties = d
return test_model
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された OpenAPI ドキュメントで問題を再現し、#1024 で導入された const のバリデーションを調べます。test-service-client/test_service_client/models/test_model.py の生成を追跡し、特に Foo スキーマの _parse_foo 関数を確認します。バリデーションが最初の const を処理した後に戻るのではなく、両方の const の候補を考慮し、回帰テストでこの例をカバーできれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- openapi, python
- 領域
- tooling
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100