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,并将其数据类型和反序列化导入与 schema 以及正确生成的 customer envelope 进行比较。当交易数据解析为 TransactionEntity 且不再出现 CustomerEntity 状态错误时,即表示完成。

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

评估

技术栈
python
领域
tooling
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

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