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

Model import shadows HTTP `Client` import when a schema is named `Client`

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

还没有人认领这个 Issue。

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

描述

Describe the bug

When an OpenAPI spec contains a schema named Client, the generated endpoint modules import both the HTTP Client class and the Client model class under the same name. The model import silently shadows the HTTP client import, making Client in type annotations refer to the wrong class.
endpoint_module.py.jinja unconditionally emits:

from ...client import AuthenticatedClient, Client

Then endpoint.relative_imports adds model imports. When a model is also named Client, the result is:

from ...client import AuthenticatedClient, Client   # HTTP client
from ...models.client import Client                  # model — shadows the line above

This causes Client in _parse_response and _build_response type annotations to resolve to the model instead of the HTTP client class.

OpenAPI Spec File

Minimal reproduction:

openapi: "3.0.3"
info:
  title: Minimal repro
  version: "1.0"
paths:
  /api/clients/{id}:
    patch:
      operationId: update_client
      tags:
        - client
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Client"
components:
  schemas:
    Client:
      type: object
      properties:
        name:
          type: string

In the generated api/client/update_client.py, line 6 imports Client from ...client and line 10 re-imports Client from ...models.client, shadowing it.

Desktop (please complete the following information):

  • OS: macOS 15 (ARM64)
  • Python Version: 3.13
  • openapi-python-client version: 0.27.1 (also verified on 0.28.3)

Additional context

We worked around this by using custom templates.

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 endpoint_module.py.jinja 和 endpoint.relative_imports 背后的逻辑开始,然后根据 issue 生成最小的 OpenAPI 规范。检查生成的 api/client/update_client.py 中的 imports,以及 _parse_response 和 _build_response 的注解。当名为 Client 的 schema 不再导致 HTTP Client 引用被遮蔽时,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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