openapi-generators / openapi-generators/openapi-python-client

Incorrect `const` property validation

未關閉
#1,044 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Python
星號
2k
分支
293
平均合併
34 分鐘
30 天內合併 PR
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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先使用提供的 OpenAPI 文件重現問題,並檢查 #1024 中引入的 const 驗證。追蹤 test-service-client/test_service_client/models/test_model.py 的產生過程,尤其是 Foo schema 的 _parse_foo 函式。完成標準是驗證同時考量兩個 const 選項,而不是在第一個選項後返回,並且有一個回歸測試涵蓋此範例。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
openapi, python
領域
tooling
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。