openapi-generators / openapi-generators/openapi-python-client
Support for multiple content types in error response
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 2k
- 分支
- 293
- 平均合併
- 34 分鐘
- 30 天內合併 PR
- 1
描述
Describe the bug
Similar to previous issues about multiple content types support like https://github.com/openapi-generators/openapi-python-client/issues/453 or https://github.com/openapi-generators/openapi-python-client/issues/1004
For a single endpoint, the same error code can be returned by different piece of software. For example, an application can return error details as application/json but have some infra elements in front (cloud gateway/load balancer/reverse proxy/firewall) that can block request and also return their own errors in their own format text/json/xml (usually for network errors 401,403,502,503,504).
In my use case, I realized a firewall can sometimes block requests and returns a text/plain with a string Access Forbidden. So I'm trying to represent that in my OpenAPI spec so the generated client can handle these errors properly without crashing, and this works for some of the other openapi generators. But it doesn't work well with this python client, because the content type is not validated in the generated code. It only depends on the first content declared in the schema and seems to ignore the other ones.
So I'm getting the following implementation:
if response.status_code == 403:
response_403 = APIError.from_dict(response.json())
Which works for json error, but json parsing throw an exception if it's something else (like text/plain)
And if I declare text/plain before application/json, I get:
if response.status_code == 403:
response_403 = response.text
But in that case, I'm losing any detail about the json error.
What I expect here, is something similar to the oapi-codegen golang implementation.
If there are multiple content types defined for an error code, check that the response content type match before handling it.
So each content type can coexist and be handled separately.
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 403:
// this is a json error
var dest APIError
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
return nil, err
}
response.JSON403 = &dest
case rsp.StatusCode == 403:
... // default case for text/plain error
OpenAPI Spec File
403:
description: Forbidden
content:
application/json:
schema:
$ref: '#/components/schemas/APIError'
text/plain:
schema:
type: string
Desktop (please complete the following information):
- OS: macOS 14.7.4
- Python Version: 3.11.88
- openapi-python-client version: 0.24.3
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
使用提供的、包含 application/json 和 text/plain 的 OpenAPI 403 回應,重現產生的用戶端目前的處理方式。檢查產生的用戶端錯誤回應路徑,並在可用時執行相關的用戶端產生與回應測試。完成的標準是,產生的用戶端根據回應的 Content-Type 選擇處理方式,讓 JSON 詳情在不嘗試將 text/plain 解析為 JSON 的情況下仍可用。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- api
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100