-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Python] Client code is wrong when an endpoint accepts multiple MediaTypes #440
Comments
@ackintosh, unfortunately, it does not seem to be fixed in 3.1.0. I still have an Accept header being |
Oh... 😢 Could you please check the generated file (Below is part of files generated from the simplified spec)
There should be the difference between 3.0.3L96 body_params = None
# HTTP header `Accept`
header_params['Accept'] = self.api_client.select_header_accept(
['application/octet-stream''application/json']) # noqa: E501 (A comma to be present between 3.1.0L96 body_params = None
# HTTP header `Accept`
header_params['Accept'] = self.api_client.select_header_accept(
['application/octet-stream', 'application/json']) # noqa: E501 What about your |
The generated code seems correct in However, the code in def select_header_accept(self, accepts):
"""Returns `Accept` based on an array of accepts provided.
:param accepts: List of headers.
:return: Accept (e.g. application/json).
"""
if not accepts:
return
accepts = [x.lower() for x in accepts]
if 'application/json' in accepts:
return 'application/json'
else:
return ', '.join(accepts) It removes all media types except That's why I still have a 406 (Not accepted) status code. |
Any update? |
Sorry for late reply. 💦
Maybe it is not good. (but currently most of the generated clients (even other language) has same issue.) My understanding about why the API client removes all media types except API client treats the response data as JSON so API client sets My ideaI think Quality values will be good solution. The following Accept header allows all media types and request that the response be returned as
The solution requires some works (template improvement), I'll file a PR if the idea gets someone's 👍 . |
Instead of removing mediatypes from Accept, we should just put application/json in first position. |
There is a same problem in Go(-experimental). // selectHeaderAccept join all accept types and return
func selectHeaderAccept(accepts []string) string {
if len(accepts) == 0 {
return ""
}
if contains(accepts, "application/json") {
return "application/json"
}
return strings.Join(accepts, ",")
} when using a param with responses:
"200":
description: OK
content:
application/json;param=foo:
schema:
$ref: "#/components/schemas/OK"
"400":
description: Bad Request
content:
application/json:
schema:
$ref: "#/components/schemas/Error400" I would propose to remove the special treatment for |
@spacether I don't believe #7427 fixes this issue as reported. #7427 fixes the python data type of the return value of responses (e.g. Is the return value a primitive type or model). This issue addresses the content-type format of the raw response (e.g. is the response "application/json" or "text/plain"). This format has implications in response deserialization. @ackintosh in #728 identifies this issue from the spec perspective (the Accept header should not be only "application/json"), but as mentioned earlier in #440 (comment), the deserialization code must also understand how to read the response. |
I see #10978 explicitly passes the response content-type around which provides the framework to the client code to handle different content-types. |
This code has been cleaned up in python-experimental. For client endpoints:
One can now use python-experimental; it was added in #8325 |
chore(deps): update dependency @types/jest to v27
Description
My OpenApi 3.0 specification contains an endpoint for which the response media type can be
application/octet-stream
or, if the query cannot be completed,application/json
.The generated Python client code builds a query with a MediaType being the concatenation of both:
application/octet-streamapplication/json
openapi-generator version
3.0.3
OpenAPI declaration file content or url
Below is my (simplified) OpenApi specification:
Command line used for generation
I call the generator directly from my Java application:
Python test file
Running this script with the generated
openapi_client
results in the following output:
The text was updated successfully, but these errors were encountered: