openapi-generators / openapi-generators/openapi-python-client
union types and nullables can create unnecessary class suffixes (and related problems)
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 2k
- 派生
- 293
- 平均合并
- 34 分钟
- 30 天内合并 PR
- 1
描述
Describe the bug
In some valid specs, the use of anyOf, oneOf, nullable, and/or type as a list, can cause generated class names to have unnecessary suffixes (and, sometimes, for spurious extra copies of classes to appear). These problems seem to all ultimately come from the behavior of UnionProperty.
The problem cases are all included in the attached specs files. Looking at the code generated from these specs—
in both 3.0 & 3.1:
ExampleModel.nullableObjectWithOneOfcorrectly has the typeUnion[MyObject, None, Unset].ExampleModel.inlineNullableObjectgenerates a model class calledExampleModelInlineNullableObjectType0. It should be justExampleModelInlineNullableObject, since there are no other named types for this property.- Similarly, the
MyEnumschema, which is a nullable enum, generates the classMyEnumType1even though there is noType0. - The
MyEnumWithExplicitTypeschema is the same asMyEnumexcept it specifically indicatestype, and the result is a bit wilder: it generates enum classesMyEnumWithExplicitTypeType1,MyEnumWithExplicitType2Type1, andMyEnumWithExplicitType3Type1, all of which are exactly the same.
only applicable to 3.1:
ExampleModel.nullableObjectWithExplicitTypes, which is the same asnullable_object_with_one_ofexcept that it also (unnecessarily, but validly) specifiestype: ["object", "null"], also works correctly.ExampleModel.one_of_enums_with_explicit_types, which combines a string enum with an int enum and specifies `type: ["string", "integer"], has the problem where three classes are created for each enum.
OpenAPI Spec Files
3.0: https://gist.github.com/eli-bl/7bea406525fb3b1452a71781ada5c1c0
3.1: https://gist.github.com/eli-bl/c03c88eb69312053e0122d1d8a06c2a0
Desktop (please complete the following information):
- OS: macOS 14.5
- Python Version: 3.8.15
- openapi-python-client version 0.21.5
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 UnionProperty 的行为入手,并根据链接的 OpenAPI 3.0 和 3.1 规范文件生成客户端。将生成的类名和重复模型与 issue 中描述的示例进行比较。当 nullable 和联合 schema 不再接收不必要的后缀,也不再创建相同的额外类时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- openapi, python
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100