-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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] Add option to select content-type using body or force it for… #10686
Conversation
Could you review this PR? Thanks in advance! |
@@ -545,10 +545,12 @@ class ApiClient(object): | |||
else: | |||
return ', '.join(accepts) | |||
|
|||
def select_header_content_type(self, content_types): | |||
def select_header_content_type(self, content_types, method=None, body=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have you added the parameters method and body here when they re unused in the method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this question. To be backward compatible I don't provide an extra code here but these parameters help me to do it later (patch or subclass the ApiClient).
But maybe we can "break" it a little and add support for JSON Patch:
if method == 'PATCH' and isinstance(body, list) and 'application/json-patch+json' in content_types:
return 'application/json-patch+json'
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't make sense to add parameters that are not used by our code because other users want to override them.
Why not pass in _content_type = 'application/json-patch+json' for your mentioned case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be more precisely - I use openapi-generator to build my client library. I'd like to prepare my own select_header_content_type which has a custom detection and uses the body and method (for instance I can check if it's json or not, it contains list or not etc.). Even if this original method doesn't use them, my method can.
My end-user, who uses my library, can pass _content_type to manually force the content-type, as you said and it's also important.
In this PR I'd like to provide these two options - forcing content-type and possibility to provide custom detection/selection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've decided to add support for 'application/json-patch+json' where these parameters are used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I don't like adding parameters that our code base that we don't use but I understand why you need them so adding them is okay.
Can you add a test that patches select_header_content_type and verifies that it is called with all of your needed inputs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I've just added tests.
This looks like a great feature addition, thanks! |
The circleCI error is unrelated:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great; thank you for writing it and adding those tests!
Thanks! |
Looks like this breaks the build due to import error
Ref: https://app.travis-ci.com/github/OpenAPITools/openapi-generator/builds/242237307 @tomplus can you please take a look when you've time? (travis ci is NOT enabled for PR due to credit limits) |
Ups! Sorry about that. I'll fix it ASAP. |
In this PR I introduce a new parameter
_content_type
to api calls and changeselect_header_content_type
to get access to the body of the request. It helps to set a valid content-type for sending requests in some cases. Current logic is the same, but the new method can be easily overridden/patched to perform better detection. What's more, users can force _content_type if necessary.Context - some APIs (eg. kubernetes, kubernetes-client/python#959, tomplus/kubernetes_asyncio#68) define their own type of content-type (application/json-patch+json, application/strategic-merge-patch+json) where current logic doesn’t work and it’s very difficult to deal with it.
PR
python-legacy
, I’ll prepare similar forpython
later.#8116
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
(5.3.0),6.0.x
@taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10) @Jyhess (2019/01) @arun-nalla (2019/11) @spacether (2019/11), please take a look.
Thank you.