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

Multiple body-types in an endpoint with same model (referenced via `$ref`) results in ambiguity in generated code

Đang mở
#1,004 1 bình luận 5 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ả

Python Version OpenAPI Client Generator Version
3.11.6 0.18.0

Actual Behavior

When an endpoint has multiple body types, each referencing the same model (via a $ref key), the generator generates the same class name for each body types, which results then in the following generated code:

def _get_kwargs(
    # ...
):
    # ...

    if isinstance(body, WritableJournalEntryRequest):
        _json_body = body.to_dict()
 
        _kwargs["json"] = _json_body
        headers["Content-Type"] = "application/json"
    if isinstance(body, WritableJournalEntryRequest):
        _files_body = body.to_multipart()
 
        _kwargs["files"] = _files_body
        headers["Content-Type"] = "multipart/form-data"
 
    # ...

As you can see, both if-statement's conditions will resolve to True, therefore only the last body type will be used.

Desired Behavior

The body-type should be part of the generated class name to avoid such ambiguities.
Or an if/elseif/else should be used in the template (which would result in the first one being applied ONLY).

OpenAPI Spec File

The OpenAPI spec is from Netbox, but here is the relevant snippet:

"requestBody": {
    "content": {
        "application/json": {
            "schema": {
                "$ref": "#/components/schemas/WritableJournalEntryRequest"
            }
        },
        "multipart/form-data": {
            "schema": {
                "$ref": "#/components/schemas/WritableJournalEntryRequest"
            }
        }
    },
    "required": true
},

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 bằng cách lần theo cách generator xử lý các mục content của OpenAPI requestBody và tạo ra hàm _get_kwargs được sinh. Tái hiện vấn đề bằng đoạn JSON được cung cấp, sau đó bổ sung coverage cho cả application/json và multipart/form-data; hoàn thành khi client được sinh chọn đúng cách xử lý body mà không có các điều kiện giống hệt nhau gây mơ hồ.

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

Đánh giá

Công nghệ
openapi, python
Lĩnh vực
api, tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/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.