[python] fix deserialize on basic str fails #18800
[python] fix deserialize on basic str fails #18800wing328 merged 9 commits intoOpenAPITools:masterfrom
Conversation
| self.assertEqual(api_response, "{}") # assertion to ensure {} is sent in the body | ||
|
|
||
| api_response = api_instance.test_echo_body_tag_response_string(None) | ||
| self.assertEqual(api_response, "") # assertion to ensure emtpy string is sent in the body |
There was a problem hiding this comment.
This fails because None is not a json.
This test is erroneous and has been removed.
There was a problem hiding this comment.
the test is for optional body parameter (e.g. Pet). shall we keep it instead?
There was a problem hiding this comment.
the test is for optional body parameter (e.g. Pet).
If that's the case, this test should be performed in petstore_api instead of echo_api.
Since an empty string is not in JSON format, it conflicts with the Content-Type: application/json in the response header.
Alternatively, is there an option to change the response header of echo_api to Content-Type: text/plain ?
There was a problem hiding this comment.
Using Pet just an example. It can be any models (which will be serialized to JSON string before sending to the server).
Since an empty string is not in JSON format, it conflicts with the Content-Type: application/json in the response header.
Understood as confirmed in https://stackoverflow.com/questions/40614416/is-empty-body-correct-if-content-type-is-application-json. but please take a look at axios/axios#4146 as well, which is also a "use case" presented to use previously (and that's why we've added a test to cover it)
With this change, if None is provided, what will be sent to the server? just empty JSON string "" ?
There was a problem hiding this comment.
Using Pet just an example. It can be any models (which will be serialized to JSON string before sending to the server).
In this case, None is not serialized to Pet. Therefore, it is not serialized into a JSON string before being sent to the server. Only empty json will be sent.
but please take a look at axios/axios#4146 as well, which is also a "use case" presented to use previously (and that's why we've added a test to cover it)
Ok, add a process to prevent json deserialize if the response is empty.
With this change, if None is provided, what will be sent to the server? just empty JSON string "" ?
This change does not change what is sent to the server. An empty json is sent.
Currently, the client may send an empty json request.
You are correct, response should also be allowed.
There was a problem hiding this comment.
Allow empty json in response
e77545e#diff-c1d7ea3a1de60effaa5908cee1be9a7e2fd420bdb75a2e2a28a6c3290380192dR411-R414
| try: | ||
| data = json.loads(response_text) | ||
| except ValueError: | ||
| data = response_text |
There was a problem hiding this comment.
None for content_type is allowed to maintain backward compatibility, but this is deprecated and may return an incorrect response.
multani
left a comment
There was a problem hiding this comment.
I'm not sure about the startswith() tests for content-type, shouldn't they be plain == instead?
|
Thanks for reviewing.
|
|
Can you review it again? |
|
merged. thanks for the fix. |
fix #18774
This PR is another approach to what I changed in #18069.
PR checklist
Commit all changed files.
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*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming 7.6.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)@cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) @multani (2023/10)