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 摘要。