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

An unexpected model is generated when server returns dictionary

未關閉
#610 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

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

描述

Describe the bug
I have a web API implementer with fastAPI. I have a function that make a POST request to the server I work with. This function returns dictionary that maps string to an object called "Clipon".

@router.get("/clipon/{version}", tags=["clipon"], status_code=status.HTTP_200_OK, response_model=Dict[str, CliponMetadata])
async def get_clipon_metadata_by_clip_name_list(version: str, clip_list: List[str]):
    try:
        res = await db_client.get_by_clip_name_list(clip_list=clip_list, version=version)
    except AssertionError as ex:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(ex))
    return {clip.clip_name: clip for clip in res}

When generating the models with openapi-python-client, what is generated is a model names "clipon_metadata" (as expected), and also another unexpected model with a long name:
image
It's full name is "GetCliponMetadataByClipNameListCliponVersionPostResponseGetCliponMetadataByClipNameListCliponVersionPost".

I don't understand why this model is created. For example, when I change this function to return List[CliponMetadata], only the "clipon_metadata" model is created.

In this behavior, when the client make this POST request, what it gets is A response object (also generated automatically), which in it's "parsed" attribute contains the unwanted object, and the "additional_properties" attribute of "parsed" holds the desired dictionary:

image

Expected behavior
I'd like that only the "clipon"metadata" model will be generated.

Desktop:

  • OS: Linux (Ubuntu)
  • Python Version: 3.9.1
  • openapi-python-client version: 0.11.1

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

首先重現帶有 response_model=Dict[str, CliponMetadata] 的 FastAPI 路由 get_clipon_metadata_by_clip_name_list,並檢查產生的回應模型和解析後的 additional_properties。將其與 List[CliponMetadata] 回應案例進行比較。當 dictionary 回應產生預期的 CliponMetadata 模型,且沒有不需要的長名稱 wrapper 模型時,即表示完成。

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

評估

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

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

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