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 摘要。