Generates invalid python code for seemingly correct json
還沒有人認領這個 Issue。
評估
研究方向
使用提供的 OpenAPI 片段搭配 openapi-python-client 0.28.3 重現問題,然後追蹤 nullable 基本型別 schema 的 request-body 產生流程。payload 未指定任何儲存庫檔案或測試;完成條件是產生的 endpoint 模組在語法上有效,並且能處理基本型別 request body,而不會出現報告中所示的孤立 else。
由索引模型根據 Issue 內容生成。
描述
Describe the bug
generator around primitives in request body fails
OpenAPI Spec File
I cannot share the full spec but this is an excempt:
"/api/helloworlduser/{externalId}/something": {
"patch": {
"tags": [
"UpdateHelloWorldUser"
],
"description": "Update user",
"operationId": "HelloWorldUserUpdate",
"parameters": [
{
"name": "externalId",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "number",
"format": "double",
"nullable": true
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserReadDTO"
}
}
}
}
}
}
},
this generates the file:
from http import HTTPStatus
from typing import Any, cast
from urllib.parse import quote
import httpx
from ...client import AuthenticatedClient, Client
from ...types import Response, UNSET
from ... import errors
from ...models.user_read_dto import UserReadDTO
from ...types import UNSET, Unset
from typing import cast
def _get_kwargs(
external_id: str,
*,
body: float | None | Unset = UNSET,
) -> dict[str, Any]:
headers: dict[str, Any] = {}
_kwargs: dict[str, Any] = {
"method": "patch",
"url": "/api/helloworlduser/{external_id}/something".format(external_id=quote(str(external_id), safe=""),),
}
else:
_kwargs["json"] = body
headers["Content-Type"] = "application/json"
_kwargs["headers"] = headers
return _kwargs
def _parse_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> UserReadDTO | None:
if response.status_code == 200:
response_200 = UserReadDTO.from_dict(response.json())
return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None
def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Response[UserReadDTO]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)
def sync_detailed(
external_id: str,
*,
client: AuthenticatedClient | Client,
body: float | None | Unset = UNSET,
) -> Response[UserReadDTO]:
""" Update user
Args:
external_id (str):
body (float | None | Unset):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[UserReadDTO]
"""
kwargs = _get_kwargs(
external_id=external_id,
body=body,
)
response = client.get_httpx_client().request(
**kwargs,
)
return _build_response(client=client, response=response)
def sync(
external_id: str,
*,
client: AuthenticatedClient | Client,
body: float | None | Unset = UNSET,
) -> UserReadDTO | None:
""" Update user
Args:
external_id (str):
body (float | None | Unset):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
UserReadDTO
"""
return sync_detailed(
external_id=external_id,
client=client,
body=body,
).parsed
async def asyncio_detailed(
external_id: str,
*,
client: AuthenticatedClient | Client,
body: float | None | Unset = UNSET,
) -> Response[UserReadDTO]:
""" Update user
Args:
external_id (str):
body (float | None | Unset):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[UserReadDTO]
"""
kwargs = _get_kwargs(
external_id=external_id,
body=body,
)
response = await client.get_async_httpx_client().request(
**kwargs
)
return _build_response(client=client, response=response)
async def asyncio(
external_id: str,
*,
client: AuthenticatedClient | Client,
body: float | None | Unset = UNSET,
) -> UserReadDTO | None:
""" Update user
Args:
external_id (str):
body (float | None | Unset):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
UserReadDTO
"""
return (await asyncio_detailed(
external_id=external_id,
client=client,
body=body,
)).parsed
which is a complete broken file (see the else in get_kwargs).
Desktop (please complete the following information):
- OS: mac os
- Python Version: 3.14
- openapi-python-client version: 0.28.3
- 主要語言
- Python
- 星號
- 2k
- 分支
- 293
- 平均合併
- 34 分鐘
- 30 天內合併 PR
- 1
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
openapi-generators/openapi-python-client 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
-
難度 2/5 1-3 小時 新手友好度 65/100
-
難度 3/5 1-2 天 新手友好度 72/100
openapi-generators/openapi-python-client#1451 · 1 則留言 ·
-
難度 3/5 1-2 天 新手友好度 52/100
-
難度 3/5 1-2 天 新手友好度 64/100
openapi-generators/openapi-python-client#1435 · 1 則留言 ·
查看 openapi-generators/openapi-python-client 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
難度 2/5 1-3 小時 新手友好度 88/100
OpenHands/extensions#626 · 1 則留言 ·
-
難度 1/5 1 小時以內 新手友好度 90/100
CSCfi/sd-search-api#39 ·
-
難度 1/5 1 小時以內 新手友好度 90/100
-
難度 2/5 1-3 小時 新手友好度 68/100
StevenBlack/hosts#3255 ·