Support for multiple content types in error response
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Sử dụng response 403 OpenAPI được cung cấp với application/json và text/plain để tái hiện cách xử lý hiện tại của client được sinh ra. Kiểm tra đường dẫn error-response của client được sinh ra và chạy phần sinh client liên quan cùng các bài kiểm thử response, nếu có. Hoàn thành có nghĩa là client được sinh ra chọn cách xử lý dựa trên Content-Type của response, để các chi tiết JSON vẫn khả dụng mà không cố gắng phân tích text/plain dưới dạng JSON.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 2k
- Fork
- 293
- Merge trung bình
- 34 phút
- Pull request đã merge (30 ngày)
- 1
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openapi-generators/openapi-python-client
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
openapi-generators/openapi-python-client#1451 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 64/100
openapi-generators/openapi-python-client#1435 · 1 bình luận ·
Tất cả issue của openapi-generators/openapi-python-client
Issue tương tự
-
🐛 Bug 🔔 Pending processing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
jumpserver/jumpserver#17584 ·
-
link-check link-check:sphinx-theme
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
OpenHands/extensions#626 · 1 bình luận ·
-
Change observation tooltip text Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
CSCfi/sd-search-api#39 ·