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

Endpoints are not created for routes with Pydantic model default values

未关闭
#944 1 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Python
星标
2k
派生
293
平均合并
34 分钟
30 天内合并 PR
1

描述

Describe the bug
When attempting to generate the client for an endpoint containing a route with a default value represented by a Pydantic model, the client generation fails. Specifically, the issue arises when the default value is set as a Pydantic model, such as the example below:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()
class Cake(BaseModel):
    slices: int

@app.post("/eat_cake")
async def eat_cake(cake: Cake | None = Cake(slices=20)):
    return {"message": f"Eating {cake.slices} slices of cake!"}

The generated client results in the following error message:

Warning(s) encountered while generating. Client was generated, but some pieces may be missing
WARNING parsing POST /graph_study/{customer_name}/distinct_identities/distinct_to_base/eat_cake within default. Endpoint will not be generated.
Value {'slices': 20} is not valid, only None is allowed

This issue is caused by a condition not being met in the union.py file.

To Reproduce

  1. Define a FastAPI app with a route containing a Pydantic model with a default value.
  2. Attempt to generate the client using openapi-python-client version 0.17.2.

OpenAPI Spec File

{
  "openapi": "3.0.2",
  "info": {
    "title": "FastAPI",
    "version": "1.0"
  },
  "paths": {
    "/cake_eater/eat_cake": {
      "post": {
        "summary": "Eat Cake",
        "operationId": "eat_cake_cake_eater___eat_cake_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "anyOf": [
                  {
                    "$ref": "#/components/schemas/Cake"
                  },
                  {
                    "type": "null"
                  }
                ],
                "title": "Cake",
                "default": {
                  "slices": 20
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HTTPValidationError"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Cake": {
        "properties": {
          "slices": {
            "type": "integer",
            "title": "Slices"
          }
        },
        "type": "object",
        "required": [
          "slices"
        ],
        "title": "Cake"
      },
      "HTTPValidationError": {
        "properties": {
          "detail": {
            "items": {
              "$ref": "#/components/schemas/ValidationError"
            },
            "type": "array",
            "title": "Detail"
          }
        },
        "type": "object",
        "title": "HTTPValidationError"
      },
      "ValidationError": {
        "properties": {
          "loc": {
            "items": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "integer"
                }
              ]
            },
            "type": "array",
            "title": "Location"
          },
          "msg": {
            "type": "string",
            "title": "Message"
          },
          "type": {
            "type": "string",
            "title": "Error Type"
          }
        },
        "type": "object",
        "required": [
          "loc",
          "msg",
          "type"
        ],
        "title": "ValidationError"
      }
    }
  }
}

Desktop (please complete the following information):

  • OS: Windows 11
  • Python Version: 3.11.1
  • openapi-python-client version 0.17.2

Additional context

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 openapi_python_client/parser/properties/union.py 第 87 行附近开始,使用提供的 OpenAPI 规范或 FastAPI 示例重现该失败。跟踪 union 默认值的验证过程,然后确认 endpoint 已成功生成,而不是被省略。

由索引模型根据 Issue 内容生成。

评估

技术栈
fastapi, python
领域
api, devtools
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。