fix(python,asyncio): multipart form data serialization (#19302)

* fix: object serialization for multipart requests

This PR is essentially
<https://github.com/OpenAPITools/openapi-generator/pull/18140> but for
the asyncio client.

* fix: int serialization for multipart requests

urllib3 handles serializing ints in post params (ref 1), while asyncio
explicitly does not (ref 2).

ref 1: <9316764e90/src/urllib3/filepost.py (L75-L76)>
ref 2: <https://github.com/aio-libs/aiohttp/issues/920>

* test: new fake multipart endpoint with files and body

* test: regression test for stringified body params

* fix: mypy tweak

* fix: FILES regeneration

* feat: object, int serialization for multipart reqs

Extends previous commits (and #18140) to cover the python-pydantic-v1
client as well.

* fix: use async with in test

* test: regression test for pydantic-v1-aiohttp

* test: add regression test to pydantic-v1

Also brings the second test in line with the first, patching
`urllib3.PoolManager.urlopen`
This commit is contained in:
Rory Schadler
2024-09-09 05:43:25 -04:00
committed by GitHub
parent 0026e15030
commit 8171648eb4
48 changed files with 2207 additions and 18 deletions

View File

@@ -112,6 +112,7 @@ docs/TestObjectForMultipartRequestsRequestMarker.md
docs/Tiger.md
docs/UnnamedDictWithAdditionalModelListProperties.md
docs/UnnamedDictWithAdditionalStringListProperties.md
docs/UploadFileWithAdditionalPropertiesRequestObject.md
docs/User.md
docs/UserApi.md
docs/WithNestedOneOf.md
@@ -233,6 +234,7 @@ petstore_api/models/test_object_for_multipart_requests_request_marker.py
petstore_api/models/tiger.py
petstore_api/models/unnamed_dict_with_additional_model_list_properties.py
petstore_api/models/unnamed_dict_with_additional_string_list_properties.py
petstore_api/models/upload_file_with_additional_properties_request_object.py
petstore_api/models/user.py
petstore_api/models/with_nested_one_of.py
petstore_api/py.typed

View File

@@ -124,6 +124,7 @@ Class | Method | HTTP request | Description
*FakeApi* | [**test_object_for_multipart_requests**](docs/FakeApi.md#test_object_for_multipart_requests) | **POST** /fake/object_for_multipart_requests |
*FakeApi* | [**test_query_parameter_collection_format**](docs/FakeApi.md#test_query_parameter_collection_format) | **PUT** /fake/test-query-parameters |
*FakeApi* | [**test_string_map_reference**](docs/FakeApi.md#test_string_map_reference) | **POST** /fake/stringMap-reference | test referenced string map
*FakeApi* | [**upload_file_with_additional_properties**](docs/FakeApi.md#upload_file_with_additional_properties) | **POST** /fake/upload_file_with_additional_properties | uploads a file and additional properties using multipart/form-data
*FakeClassnameTags123Api* | [**test_classname**](docs/FakeClassnameTags123Api.md#test_classname) | **PATCH** /fake_classname_test | To test class name in snake case
*ImportTestDatetimeApi* | [**import_test_return_datetime**](docs/ImportTestDatetimeApi.md#import_test_return_datetime) | **GET** /import_test/return_datetime | test date time
*PetApi* | [**add_pet**](docs/PetApi.md#add_pet) | **POST** /pet | Add a new pet to the store
@@ -253,6 +254,7 @@ Class | Method | HTTP request | Description
- [Tiger](docs/Tiger.md)
- [UnnamedDictWithAdditionalModelListProperties](docs/UnnamedDictWithAdditionalModelListProperties.md)
- [UnnamedDictWithAdditionalStringListProperties](docs/UnnamedDictWithAdditionalStringListProperties.md)
- [UploadFileWithAdditionalPropertiesRequestObject](docs/UploadFileWithAdditionalPropertiesRequestObject.md)
- [User](docs/User.md)
- [WithNestedOneOf](docs/WithNestedOneOf.md)

View File

@@ -40,6 +40,7 @@ Method | HTTP request | Description
[**test_object_for_multipart_requests**](FakeApi.md#test_object_for_multipart_requests) | **POST** /fake/object_for_multipart_requests |
[**test_query_parameter_collection_format**](FakeApi.md#test_query_parameter_collection_format) | **PUT** /fake/test-query-parameters |
[**test_string_map_reference**](FakeApi.md#test_string_map_reference) | **POST** /fake/stringMap-reference | test referenced string map
[**upload_file_with_additional_properties**](FakeApi.md#upload_file_with_additional_properties) | **POST** /fake/upload_file_with_additional_properties | uploads a file and additional properties using multipart/form-data
# **fake_any_type_request_body**
@@ -2446,3 +2447,75 @@ No authorization required
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
# **upload_file_with_additional_properties**
> ApiResponse upload_file_with_additional_properties(file, object=object, count=count)
uploads a file and additional properties using multipart/form-data
### Example
```python
import time
import os
import petstore_api
from petstore_api.models.api_response import ApiResponse
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject
from petstore_api.rest import ApiException
from pprint import pprint
# Defining the host is optional and defaults to http://petstore.swagger.io:80/v2
# See configuration.py for a list of all supported configuration parameters.
configuration = petstore_api.Configuration(
host = "http://petstore.swagger.io:80/v2"
)
# Enter a context with an instance of the API client
with petstore_api.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = petstore_api.FakeApi(api_client)
file = None # bytearray | file to upload
object = petstore_api.UploadFileWithAdditionalPropertiesRequestObject() # UploadFileWithAdditionalPropertiesRequestObject | (optional)
count = 56 # int | Integer count (optional)
try:
# uploads a file and additional properties using multipart/form-data
api_response = api_instance.upload_file_with_additional_properties(file, object=object, count=count)
print("The response of FakeApi->upload_file_with_additional_properties:\n")
pprint(api_response)
except Exception as e:
print("Exception when calling FakeApi->upload_file_with_additional_properties: %s\n" % e)
```
### Parameters
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**file** | **bytearray**| file to upload |
**object** | [**UploadFileWithAdditionalPropertiesRequestObject**](UploadFileWithAdditionalPropertiesRequestObject.md)| | [optional]
**count** | **int**| Integer count | [optional]
### Return type
[**ApiResponse**](ApiResponse.md)
### Authorization
No authorization required
### HTTP request headers
- **Content-Type**: multipart/form-data
- **Accept**: application/json
### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
**200** | successful operation | - |
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)

View File

@@ -0,0 +1,29 @@
# UploadFileWithAdditionalPropertiesRequestObject
Additional object
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**name** | **str** | | [optional]
## Example
```python
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject
# TODO update the JSON string below
json = "{}"
# create an instance of UploadFileWithAdditionalPropertiesRequestObject from a JSON string
upload_file_with_additional_properties_request_object_instance = UploadFileWithAdditionalPropertiesRequestObject.from_json(json)
# print the JSON string representation of the object
print UploadFileWithAdditionalPropertiesRequestObject.to_json()
# convert the object into a dict
upload_file_with_additional_properties_request_object_dict = upload_file_with_additional_properties_request_object_instance.to_dict()
# create an instance of UploadFileWithAdditionalPropertiesRequestObject from a dict
upload_file_with_additional_properties_request_object_from_dict = UploadFileWithAdditionalPropertiesRequestObject.from_dict(upload_file_with_additional_properties_request_object_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@@ -141,5 +141,6 @@ from petstore_api.models.test_object_for_multipart_requests_request_marker impor
from petstore_api.models.tiger import Tiger
from petstore_api.models.unnamed_dict_with_additional_model_list_properties import UnnamedDictWithAdditionalModelListProperties
from petstore_api.models.unnamed_dict_with_additional_string_list_properties import UnnamedDictWithAdditionalStringListProperties
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject
from petstore_api.models.user import User
from petstore_api.models.with_nested_one_of import WithNestedOneOf

View File

@@ -25,6 +25,7 @@ from pydantic import Field, StrictBool, StrictBytes, StrictFloat, StrictInt, Str
from typing import Any, Dict, List, Optional, Union
from petstore_api.models.api_response import ApiResponse
from petstore_api.models.client import Client
from petstore_api.models.enum_class import EnumClass
from petstore_api.models.file_schema_test_class import FileSchemaTestClass
@@ -36,6 +37,7 @@ from petstore_api.models.pet import Pet
from petstore_api.models.tag import Tag
from petstore_api.models.test_inline_freeform_additional_properties_request import TestInlineFreeformAdditionalPropertiesRequest
from petstore_api.models.test_object_for_multipart_requests_request_marker import TestObjectForMultipartRequestsRequestMarker
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject
from petstore_api.models.user import User
from petstore_api.api_client import ApiClient
@@ -5237,3 +5239,166 @@ class FakeApi:
_request_timeout=_params.get('_request_timeout'),
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))
@validate_arguments
def upload_file_with_additional_properties(self, file : Annotated[Union[StrictBytes, StrictStr], Field(..., description="file to upload")], object : Optional[UploadFileWithAdditionalPropertiesRequestObject] = None, count : Annotated[Optional[StrictInt], Field(description="Integer count")] = None, **kwargs) -> ApiResponse: # noqa: E501
"""uploads a file and additional properties using multipart/form-data # noqa: E501
# noqa: E501
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.upload_file_with_additional_properties(file, object, count, async_req=True)
>>> result = thread.get()
:param file: file to upload (required)
:type file: bytearray
:param object:
:type object: UploadFileWithAdditionalPropertiesRequestObject
:param count: Integer count
:type count: int
:param async_req: Whether to execute the request asynchronously.
:type async_req: bool, optional
:param _request_timeout: timeout setting for this request.
If one number provided, it will be total request
timeout. It can also be a pair (tuple) of
(connection, read) timeouts.
:return: Returns the result object.
If the method is called asynchronously,
returns the request thread.
:rtype: ApiResponse
"""
kwargs['_return_http_data_only'] = True
if '_preload_content' in kwargs:
message = "Error! Please call the upload_file_with_additional_properties_with_http_info method with `_preload_content` instead and obtain raw data from ApiResponse.raw_data" # noqa: E501
raise ValueError(message)
return self.upload_file_with_additional_properties_with_http_info(file, object, count, **kwargs) # noqa: E501
@validate_arguments
def upload_file_with_additional_properties_with_http_info(self, file : Annotated[Union[StrictBytes, StrictStr], Field(..., description="file to upload")], object : Optional[UploadFileWithAdditionalPropertiesRequestObject] = None, count : Annotated[Optional[StrictInt], Field(description="Integer count")] = None, **kwargs) -> ApiResponse: # noqa: E501
"""uploads a file and additional properties using multipart/form-data # noqa: E501
# noqa: E501
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.upload_file_with_additional_properties_with_http_info(file, object, count, async_req=True)
>>> result = thread.get()
:param file: file to upload (required)
:type file: bytearray
:param object:
:type object: UploadFileWithAdditionalPropertiesRequestObject
:param count: Integer count
:type count: int
:param async_req: Whether to execute the request asynchronously.
:type async_req: bool, optional
:param _preload_content: if False, the ApiResponse.data will
be set to none and raw_data will store the
HTTP response body without reading/decoding.
Default is True.
:type _preload_content: bool, optional
:param _return_http_data_only: response data instead of ApiResponse
object with status code, headers, etc
:type _return_http_data_only: bool, optional
:param _request_timeout: timeout setting for this request. If one
number provided, it will be total request
timeout. It can also be a pair (tuple) of
(connection, read) timeouts.
:param _request_auth: set to override the auth_settings for an a single
request; this effectively ignores the authentication
in the spec for a single request.
:type _request_auth: dict, optional
:type _content_type: string, optional: force content-type for the request
:return: Returns the result object.
If the method is called asynchronously,
returns the request thread.
:rtype: tuple(ApiResponse, status_code(int), headers(HTTPHeaderDict))
"""
_params = locals()
_all_params = [
'file',
'object',
'count'
]
_all_params.extend(
[
'async_req',
'_return_http_data_only',
'_preload_content',
'_request_timeout',
'_request_auth',
'_content_type',
'_headers'
]
)
# validate the arguments
for _key, _val in _params['kwargs'].items():
if _key not in _all_params:
raise ApiTypeError(
"Got an unexpected keyword argument '%s'"
" to method upload_file_with_additional_properties" % _key
)
_params[_key] = _val
del _params['kwargs']
_collection_formats = {}
# process the path parameters
_path_params = {}
# process the query parameters
_query_params = []
# process the header parameters
_header_params = dict(_params.get('_headers', {}))
# process the form parameters
_form_params = []
_files = {}
if _params['file'] is not None:
_files['file'] = _params['file']
if _params['object'] is not None:
_form_params.append(('object', _params['object']))
if _params['count'] is not None:
_form_params.append(('count', _params['count']))
# process the body parameter
_body_params = None
# set the HTTP header `Accept`
_header_params['Accept'] = self.api_client.select_header_accept(
['application/json']) # noqa: E501
# set the HTTP header `Content-Type`
_content_types_list = _params.get('_content_type',
self.api_client.select_header_content_type(
['multipart/form-data']))
if _content_types_list:
_header_params['Content-Type'] = _content_types_list
# authentication setting
_auth_settings = [] # noqa: E501
_response_types_map = {
'200': "ApiResponse",
}
return self.api_client.call_api(
'/fake/upload_file_with_additional_properties', 'POST',
_path_params,
_query_params,
_header_params,
body=_body_params,
post_params=_form_params,
files=_files,
response_types_map=_response_types_map,
auth_settings=_auth_settings,
async_req=_params.get('async_req'),
_return_http_data_only=_params.get('_return_http_data_only'), # noqa: E501
_preload_content=_params.get('_preload_content', True),
_request_timeout=_params.get('_request_timeout'),
collection_formats=_collection_formats,
_request_auth=_params.get('_request_auth'))

View File

@@ -116,5 +116,6 @@ from petstore_api.models.test_object_for_multipart_requests_request_marker impor
from petstore_api.models.tiger import Tiger
from petstore_api.models.unnamed_dict_with_additional_model_list_properties import UnnamedDictWithAdditionalModelListProperties
from petstore_api.models.unnamed_dict_with_additional_string_list_properties import UnnamedDictWithAdditionalStringListProperties
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject
from petstore_api.models.user import User
from petstore_api.models.with_nested_one_of import WithNestedOneOf

View File

@@ -0,0 +1,83 @@
# coding: utf-8
"""
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
""" # noqa: E501
from __future__ import annotations
import pprint
import re # noqa: F401
import json
from typing import Any, Dict, Optional
from pydantic import BaseModel, StrictStr
class UploadFileWithAdditionalPropertiesRequestObject(BaseModel):
"""
Additional object # noqa: E501
"""
name: Optional[StrictStr] = None
additional_properties: Dict[str, Any] = {}
__properties = ["name"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.dict(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
return json.dumps(self.to_dict())
@classmethod
def from_json(cls, json_str: str) -> UploadFileWithAdditionalPropertiesRequestObject:
"""Create an instance of UploadFileWithAdditionalPropertiesRequestObject from a JSON string"""
return cls.from_dict(json.loads(json_str))
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
exclude={
"additional_properties"
},
exclude_none=True)
# puts key-value pairs in additional_properties in the top level
if self.additional_properties is not None:
for _key, _value in self.additional_properties.items():
_dict[_key] = _value
return _dict
@classmethod
def from_dict(cls, obj: dict) -> UploadFileWithAdditionalPropertiesRequestObject:
"""Create an instance of UploadFileWithAdditionalPropertiesRequestObject from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return UploadFileWithAdditionalPropertiesRequestObject.parse_obj(obj)
_obj = UploadFileWithAdditionalPropertiesRequestObject.parse_obj({
"name": obj.get("name")
})
# store additional fields in additional_properties
for _key in obj.keys():
if _key not in cls.__properties:
_obj.additional_properties[_key] = obj.get(_key)
return _obj

View File

@@ -200,6 +200,8 @@ class RESTClientObject:
# Content-Type which generated by urllib3 will be
# overwritten.
del headers['Content-Type']
# Ensures that dict objects are serialized
post_params = [(a, json.dumps(b)) if isinstance(b, dict) else (a,b) for a, b in post_params]
r = self.pool_manager.request(
method, url,
fields=post_params,

View File

@@ -0,0 +1,52 @@
# coding: utf-8
"""
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
""" # noqa: E501
import unittest
import datetime
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject # noqa: E501
class TestUploadFileWithAdditionalPropertiesRequestObject(unittest.TestCase):
"""UploadFileWithAdditionalPropertiesRequestObject unit test stubs"""
def setUp(self):
pass
def tearDown(self):
pass
def make_instance(self, include_optional) -> UploadFileWithAdditionalPropertiesRequestObject:
"""Test UploadFileWithAdditionalPropertiesRequestObject
include_option is a boolean, when False only required
params are included, when True both required and
optional params are included """
# uncomment below to create an instance of `UploadFileWithAdditionalPropertiesRequestObject`
"""
model = UploadFileWithAdditionalPropertiesRequestObject() # noqa: E501
if include_optional:
return UploadFileWithAdditionalPropertiesRequestObject(
name = ''
)
else:
return UploadFileWithAdditionalPropertiesRequestObject(
)
"""
def testUploadFileWithAdditionalPropertiesRequestObject(self):
"""Test UploadFileWithAdditionalPropertiesRequestObject"""
# inst_req_only = self.make_instance(include_optional=False)
# inst_req_and_optional = self.make_instance(include_optional=True)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,57 @@
import os
import unittest
from unittest.mock import Mock, patch
import petstore_api
class TestMultipleResponseTypes(unittest.TestCase):
def setUpFiles(self):
self.test_file_dir = os.path.join(os.path.dirname(__file__), "..", "testfiles")
self.test_file_dir = os.path.realpath(self.test_file_dir)
self.test_file_path = os.path.join(self.test_file_dir, "pix.gif")
def setUp(self):
self.api_client = petstore_api.ApiClient()
self.fake_api = petstore_api.FakeApi(self.api_client)
self.setUpFiles()
def test_multipart_requests(self):
mock_resp = Mock()
mock_resp.status = 200
mock_resp.data = b"some text"
mock_resp.headers = {}
marker = petstore_api.TestObjectForMultipartRequestsRequestMarker(
name="name",
)
with patch("urllib3.PoolManager.urlopen", return_value=mock_resp):
returned = self.fake_api.test_object_for_multipart_requests(marker=marker)
assert returned is None
def test_multipart_requests_with_file_and_additional_properties(self):
mock_resp = Mock()
mock_resp.status = 200
mock_resp.data = b'{"code": 200, "type": "success", "message": "OK"}'
mock_resp.headers = {"Content-Type": "application/json"}
with patch("urllib3.PoolManager.urlopen", return_value=mock_resp):
returned = self.fake_api.upload_file_with_additional_properties(
file=self.test_file_path,
count=100,
object=petstore_api.UploadFileWithAdditionalPropertiesRequestObject(
name="foo"
),
)
# response shape is actually petstore_api.models.api_response.ApiResponse,
# but return type is annotated petstore_api.api_response.ApiResponse, thus
# the type: ignores
assert (
returned.code == 200 # type: ignore
and returned.type == "success" # type: ignore
and returned.message == "OK" # type: ignore
)
# if the request is successful, both int and dict body parameters were
# successfully serialized