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

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

Đang mở
#1,423 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
2k
Fork
293
Merge trung bình
34 phút
Pull request đã merge (30 ngày)
1

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với endpoint_module.py.jinja và logic phía sau endpoint.relative_imports, sau đó tạo đặc tả OpenAPI tối thiểu từ issue. Kiểm tra các import trong api/client/update_client.py đã tạo và các annotation của _parse_response và _build_response. Hoàn thành khi một schema có tên Client không còn khiến tham chiếu đến HTTP Client bị che khuất.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
api, tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
70/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.