Skip to content

Commit

Permalink
[python] json like str response (OpenAPITools#18069)
Browse files Browse the repository at this point in the history
* [python] json like str response

* [python] fix response deserialize

* [python] update sample

* [python] fix echo_api test quotes
  • Loading branch information
fa0311 committed Mar 11, 2024
1 parent 82fcf28 commit 678db1e
Show file tree
Hide file tree
Showing 21 changed files with 8,084 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ class ApiClient:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_text = response_data.data.decode(encoding)
return_data = self.deserialize(response_text, response_type)
if response_type in ["bytearray", "str"]:
return_data = self.__deserialize_primitive(response_text, response_type)
else:
return_data = self.deserialize(response_text, response_type)
finally:
if not 200 <= response_data.status <= 299:
raise ApiException.from_response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,75 @@ paths:
responses:
"200":
description: Get successful
/fake/return_string:
get:
tags:
- fake
summary: test returning string
operationId: fake_return_string
responses:
200:
description: OK
content:
application/json:
schema:
type: string
/fake/return_int:
get:
tags:
- fake
summary: test returning int
operationId: fake_return_int
responses:
200:
description: OK
content:
application/json:
schema:
type: integer
/fake/return_float:
get:
tags:
- fake
summary: test returning float
operationId: fake_return_float
responses:
200:
description: OK
content:
application/json:
schema:
type: number
format: float
/fake/return_boolean:
get:
tags:
- fake
summary: test returning boolean
operationId: fake_return_boolean
responses:
200:
description: OK
content:
application/json:
schema:
type: boolean
/fake/return_enum:
get:
tags:
- fake
summary: test returning enum
operationId: fake_return_enum
responses:
200:
description: OK
content:
application/json:
schema:
type: string
enum:
- a
- b
/fake/return_list_of_object:
get:
tags:
Expand Down Expand Up @@ -1412,6 +1481,49 @@ paths:
plain/text:
schema:
$ref: "#/components/schemas/EnumClass"
/fake/return_str_like_json:
get:
tags:
- fake
summary: test str like json
operationId: fake_return_str_like_json
responses:
200:
description: OK
content:
plain/text:
schema:
type: string
/fake/return_enum_like_json:
get:
tags:
- fake
summary: test enum like json
operationId: fake_return_enum_like_json
responses:
200:
description: OK
content:
plain/text:
schema:
type: string
enum:
- '{"a": "a"}'
- '{"b": "b"}'
/fake/return_byte_like_json:
get:
tags:
- fake
summary: test byte like json
operationId: fake_return_byte_like_json
responses:
200:
description: OK
content:
plain/text:
schema:
type: string
format: byte
servers:
- url: 'http://{server}.swagger.io:{port}/v2'
description: petstore server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ def response_deserialize(
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_text = response_data.data.decode(encoding)
return_data = self.deserialize(response_text, response_type)
if response_type in ["bytearray", "str"]:
return_data = self.__deserialize_primitive(response_text, response_type)
else:
return_data = self.deserialize(response_text, response_type)
finally:
if not 200 <= response_data.status <= 299:
raise ApiException.from_response(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def testBodyParameter(self):
n = openapi_client.Pet.from_dict({"name": "testing", "photoUrls": ["http://1", "http://2"]})
api_instance = openapi_client.BodyApi()
api_response = api_instance.test_echo_body_pet_response_string(n)
self.assertEqual(api_response, "{'name': 'testing', 'photoUrls': ['http://1', 'http://2']}")
self.assertEqual(api_response, '{"name": "testing", "photoUrls": ["http://1", "http://2"]}')

t = openapi_client.Tag()
api_response = api_instance.test_echo_body_tag_response_string(t)
Expand Down
5 changes: 4 additions & 1 deletion samples/client/echo_api/python/openapi_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ def response_deserialize(
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_text = response_data.data.decode(encoding)
return_data = self.deserialize(response_text, response_type)
if response_type in ["bytearray", "str"]:
return_data = self.__deserialize_primitive(response_text, response_type)
else:
return_data = self.deserialize(response_text, response_type)
finally:
if not 200 <= response_data.status <= 299:
raise ApiException.from_response(
Expand Down
2 changes: 1 addition & 1 deletion samples/client/echo_api/python/tests/test_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def testBodyParameter(self):
n = openapi_client.Pet.from_dict({"name": "testing", "photoUrls": ["http://1", "http://2"]})
api_instance = openapi_client.BodyApi()
api_response = api_instance.test_echo_body_pet_response_string(n)
self.assertEqual(api_response, "{'name': 'testing', 'photoUrls': ['http://1', 'http://2']}")
self.assertEqual(api_response, '{"name": "testing", "photoUrls": ["http://1", "http://2"]}')

t = openapi_client.Tag()
api_response = api_instance.test_echo_body_tag_response_string(t)
Expand Down
8 changes: 8 additions & 0 deletions samples/openapi3/client/petstore/python-aiohttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ Class | Method | HTTP request | Description
*FakeApi* | [**fake_outer_string_serialize**](docs/FakeApi.md#fake_outer_string_serialize) | **POST** /fake/outer/string |
*FakeApi* | [**fake_property_enum_integer_serialize**](docs/FakeApi.md#fake_property_enum_integer_serialize) | **POST** /fake/property/enum-int |
*FakeApi* | [**fake_ref_enum_string**](docs/FakeApi.md#fake_ref_enum_string) | **GET** /fake/ref_enum_string | test ref to enum string
*FakeApi* | [**fake_return_boolean**](docs/FakeApi.md#fake_return_boolean) | **GET** /fake/return_boolean | test returning boolean
*FakeApi* | [**fake_return_byte_like_json**](docs/FakeApi.md#fake_return_byte_like_json) | **GET** /fake/return_byte_like_json | test byte like json
*FakeApi* | [**fake_return_enum**](docs/FakeApi.md#fake_return_enum) | **GET** /fake/return_enum | test returning enum
*FakeApi* | [**fake_return_enum_like_json**](docs/FakeApi.md#fake_return_enum_like_json) | **GET** /fake/return_enum_like_json | test enum like json
*FakeApi* | [**fake_return_float**](docs/FakeApi.md#fake_return_float) | **GET** /fake/return_float | test returning float
*FakeApi* | [**fake_return_int**](docs/FakeApi.md#fake_return_int) | **GET** /fake/return_int | test returning int
*FakeApi* | [**fake_return_list_of_objects**](docs/FakeApi.md#fake_return_list_of_objects) | **GET** /fake/return_list_of_object | test returning list of objects
*FakeApi* | [**fake_return_str_like_json**](docs/FakeApi.md#fake_return_str_like_json) | **GET** /fake/return_str_like_json | test str like json
*FakeApi* | [**fake_return_string**](docs/FakeApi.md#fake_return_string) | **GET** /fake/return_string | test returning string
*FakeApi* | [**fake_uuid_example**](docs/FakeApi.md#fake_uuid_example) | **GET** /fake/uuid_example | test uuid example
*FakeApi* | [**test_additional_properties_reference**](docs/FakeApi.md#test_additional_properties_reference) | **POST** /fake/additionalProperties-reference | test referenced additionalProperties
*FakeApi* | [**test_body_with_binary**](docs/FakeApi.md#test_body_with_binary) | **PUT** /fake/body-with-binary |
Expand Down
Loading

0 comments on commit 678db1e

Please sign in to comment.