openapi-generators / openapi-generators/openapi-python-client
Model named `Client` causes errors
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 2k
- 派生
- 293
- 平均合并
- 34 分钟
- 30 天内合并 PR
- 1
描述
Describe the bug
If a model of the Openapi App is called Client this name collides with the client.Client object in imports.
OpenAPI Spec File
{
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/client": {
"get": {
"summary": "Client",
"operationId": "client_client_get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Client"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Client": {
"properties": {
"foo": {
"type": "string",
"title": "Foo"
},
"bar": {
"type": "integer",
"title": "Bar"
}
},
"type": "object",
"required": [
"foo",
"bar"
],
"title": "Client"
}
}
}
}
Desktop (please complete the following information):
- OS: [e.g. Ubuntu 22.04]
- Python Version: [e.g. 3.10.12]
- openapi-python-client version [e.g. 0.19.1]
Additional context
The generator outputs:
Generating fast-api-client
Error(s) encountered while generating, client was not created
ruff failed
fast_api_client/api/default/client_client_get.py:8:30: F811 Redefinition of unused `Client` from line 7
Found 16 errors (15 fixed, 1 remaining).
In the relevant code there are two imports:
from ...client import AuthenticatedClient, Client
from ...models.client import Client
As hinted by ruff' error message there are two imports that load a Client object but they are loading different objects.
If you ty to use the generated code:
from fast_api_client import Client
from fast_api_client.api.default import client_client_get
with Client(base_url="http://localhost:8000") as client:
data = client_client_get.sync(client=client)
print(data)
It will still work properly as the only Client type that is actually used at runtime is the one from models which is imported last (the Client from client is only used for type-hinting).
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
使用提供的 OpenAPI 规范重现该问题,然后检查生成的 fast_api_client/api/default/client_client_get.py 中的导入。生成的模型类型和客户端类型不再冲突,并且 ruff 在没有 F811 错误的情况下通过时,修复就完成了。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- api, tooling
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100