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

Bug Report: Incorrect Model Reference in Generated Paginated Envelope

未關閉
#1,390 0 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
Python
星號
2k
分支
293
平均合併
34 分鐘
30 天內合併 PR
1

描述

Bug Report: Incorrect Model Reference in Generated Paginated Envelope

Summary

openapi-python-client incorrectly generates PaginatedTransactionEnvelope with CustomerEntity instead of TransactionEntity, despite the OpenAPI schema correctly specifying TransactionEntity.

Environment

  • openapi-python-client version: 0.28.1
  • Python version: 3.13
  • Generation command:
    openapi-python-client generate \
        --path schema.yaml \
        --output-path ./generated \
        --meta none
    

Expected Behavior

The generated PaginatedTransactionEnvelope should use TransactionEntity based on this schema definition:

PaginatedTransactionEnvelope:
  allOf:
  - $ref: '#/components/schemas/PaginatedSuccessEnvelope'
  - type: object
    properties:
      data:
        type: array
        items:
          $ref: '#/components/schemas/TransactionEntity'

Expected generated code:

if TYPE_CHECKING:
    from ..models.transaction_entity import TransactionEntity
    from ..models.links import Links

@_attrs_define
class PaginatedTransactionEnvelope:
    timestamp: datetime.datetime
    path: str
    data: list[TransactionEntity]  # ✓ Correct
    links: Links

Actual Behavior

The generator produces code using CustomerEntity instead:

if TYPE_CHECKING:
    from ..models.customer_entity import CustomerEntity  # ✗ Wrong!
    from ..models.links import Links

@_attrs_define
class PaginatedTransactionEnvelope:
    timestamp: datetime.datetime
    path: str
    data: list[CustomerEntity]  # ✗ Wrong!
    links: Links
    
@classmethod
def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
    from ..models.customer_entity import CustomerEntity  # ✗ Wrong!
    from ..models.links import Links
    
    # ...
    for data_item_data in _data:
        data_item = CustomerEntity.from_dict(data_item_data)  # ✗ Wrong!
        data.append(data_item)

Impact

This causes runtime errors when parsing API responses:

ValueError: 'failed' is not a valid CustomerEntityStatus

The parser attempts to deserialize transaction data as customer data, failing when transaction-specific status values don't exist in the CustomerEntityStatus enum.

Additional Context

The schema also defines PaginatedCustomerEnvelope (which correctly uses CustomerEntity). The generator may be incorrectly reusing or caching the entity type across similar paginated envelope structures.

Related schema definitions:

PaginatedCustomerEnvelope:
  allOf:
  - $ref: '#/components/schemas/PaginatedSuccessEnvelope'
  - type: object
    properties:
      data:
        type: array
        items:
          $ref: '#/components/schemas/CustomerEntity'

PaginatedTransactionEnvelope:
  allOf:
  - $ref: '#/components/schemas/PaginatedSuccessEnvelope'
  - type: object
    properties:
      data:
        type: array
        items:
          $ref: '#/components/schemas/TransactionEntity'

Workaround

We've added a post-generation fix script:

sed -i '' 's/from ..models.customer_entity import CustomerEntity/from ..models.transaction_entity import TransactionEntity/g' "$FILE"
sed -i '' 's/list\[CustomerEntity\]/list[TransactionEntity]/g' "$FILE"
sed -i '' 's/CustomerEntity\.from_dict/TransactionEntity.from_dict/g' "$FILE"

Reproduction

The issue appears when:

  1. Multiple paginated envelopes are defined using allOf with shared base schemas
  2. Each envelope should use different entity types in the data array
  3. The generator confuses the entity types between envelopes

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從提供的 schema.yaml 開始,使用兩個分頁 envelope 定義重現產生命令。檢查產生的 PaginatedTransactionEnvelope,並將其資料型別和反序列化 import 與 schema 以及正確產生的 customer envelope 進行比較。當交易資料解析為 TransactionEntity 且不再出現 CustomerEntity 狀態錯誤時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
tooling
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。