Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Fixes multipart form content disposition bug #67

Merged
merged 5 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1399,24 +1399,24 @@ class RequestBody(StyleFormSerializer, JSONDetector):

def __multipart_json_item(self, key: str, value: Schema) -> RequestField:
json_value = self.__json_encoder.default(value)
return RequestField(name=key, data=json.dumps(json_value), headers={'Content-Type': 'application/json'})
request_field = RequestField(name=key, data=json.dumps(json_value))
request_field.make_multipart(content_type='application/json')
return request_field

def __multipart_form_item(self, key: str, value: Schema) -> RequestField:
if isinstance(value, str):
return RequestField(name=key, data=str(value), headers={'Content-Type': 'text/plain'})
request_field = RequestField(name=key, data=str(value))
request_field.make_multipart(content_type='text/plain')
elif isinstance(value, bytes):
return RequestField(name=key, data=value, headers={'Content-Type': 'application/octet-stream'})
request_field = RequestField(name=key, data=value)
request_field.make_multipart(content_type='application/octet-stream')
elif isinstance(value, FileIO):
request_field = RequestField(
name=key,
data=value.read(),
filename=os.path.basename(value.name),
headers={'Content-Type': 'application/octet-stream'}
)
# TODO use content.encoding to limit allowed content types if they are present
request_field = RequestField.from_tuples(key, (os.path.basename(value.name), value.read()))
value.close()
return request_field
else:
return self.__multipart_json_item(key=key, value=value)
request_field = self.__multipart_json_item(key=key, value=value)
return request_field

def __serialize_multipart_form_data(
self, in_data: Schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1389,24 +1389,24 @@ def __serialize_text_plain(in_data: typing.Any) -> typing.Dict[str, str]:

def __multipart_json_item(self, key: str, value: Schema) -> RequestField:
json_value = self.__json_encoder.default(value)
return RequestField(name=key, data=json.dumps(json_value), headers={'Content-Type': 'application/json'})
request_field = RequestField(name=key, data=json.dumps(json_value))
request_field.make_multipart(content_type='application/json')
return request_field

def __multipart_form_item(self, key: str, value: Schema) -> RequestField:
if isinstance(value, str):
return RequestField(name=key, data=str(value), headers={'Content-Type': 'text/plain'})
request_field = RequestField(name=key, data=str(value))
request_field.make_multipart(content_type='text/plain')
elif isinstance(value, bytes):
return RequestField(name=key, data=value, headers={'Content-Type': 'application/octet-stream'})
request_field = RequestField(name=key, data=value)
request_field.make_multipart(content_type='application/octet-stream')
elif isinstance(value, FileIO):
request_field = RequestField(
name=key,
data=value.read(),
filename=os.path.basename(value.name),
headers={'Content-Type': 'application/octet-stream'}
)
# TODO use content.encoding to limit allowed content types if they are present
request_field = RequestField.from_tuples(key, (os.path.basename(value.name), value.read()))
value.close()
return request_field
else:
return self.__multipart_json_item(key=key, value=value)
request_field = self.__multipart_json_item(key=key, value=value)
return request_field

def __serialize_multipart_form_data(
self, in_data: Schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1389,24 +1389,24 @@ def __serialize_text_plain(in_data: typing.Any) -> typing.Dict[str, str]:

def __multipart_json_item(self, key: str, value: Schema) -> RequestField:
json_value = self.__json_encoder.default(value)
return RequestField(name=key, data=json.dumps(json_value), headers={'Content-Type': 'application/json'})
request_field = RequestField(name=key, data=json.dumps(json_value))
request_field.make_multipart(content_type='application/json')
return request_field

def __multipart_form_item(self, key: str, value: Schema) -> RequestField:
if isinstance(value, str):
return RequestField(name=key, data=str(value), headers={'Content-Type': 'text/plain'})
request_field = RequestField(name=key, data=str(value))
request_field.make_multipart(content_type='text/plain')
elif isinstance(value, bytes):
return RequestField(name=key, data=value, headers={'Content-Type': 'application/octet-stream'})
request_field = RequestField(name=key, data=value)
request_field.make_multipart(content_type='application/octet-stream')
elif isinstance(value, FileIO):
request_field = RequestField(
name=key,
data=value.read(),
filename=os.path.basename(value.name),
headers={'Content-Type': 'application/octet-stream'}
)
# TODO use content.encoding to limit allowed content types if they are present
request_field = RequestField.from_tuples(key, (os.path.basename(value.name), value.read()))
value.close()
return request_field
else:
return self.__multipart_json_item(key=key, value=value)
request_field = self.__multipart_json_item(key=key, value=value)
return request_field

def __serialize_multipart_form_data(
self, in_data: Schema
Expand Down
22 changes: 11 additions & 11 deletions samples/openapi3/client/petstore/python/petstore_api/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,24 +1398,24 @@ def __serialize_text_plain(in_data: typing.Any) -> typing.Dict[str, str]:

def __multipart_json_item(self, key: str, value: Schema) -> RequestField:
json_value = self.__json_encoder.default(value)
return RequestField(name=key, data=json.dumps(json_value), headers={'Content-Type': 'application/json'})
request_field = RequestField(name=key, data=json.dumps(json_value))
request_field.make_multipart(content_type='application/json')
return request_field

def __multipart_form_item(self, key: str, value: Schema) -> RequestField:
if isinstance(value, str):
return RequestField(name=key, data=str(value), headers={'Content-Type': 'text/plain'})
request_field = RequestField(name=key, data=str(value))
request_field.make_multipart(content_type='text/plain')
elif isinstance(value, bytes):
return RequestField(name=key, data=value, headers={'Content-Type': 'application/octet-stream'})
request_field = RequestField(name=key, data=value)
request_field.make_multipart(content_type='application/octet-stream')
elif isinstance(value, FileIO):
request_field = RequestField(
name=key,
data=value.read(),
filename=os.path.basename(value.name),
headers={'Content-Type': 'application/octet-stream'}
)
# TODO use content.encoding to limit allowed content types if they are present
request_field = RequestField.from_tuples(key, (os.path.basename(value.name), value.read()))
value.close()
return request_field
else:
return self.__multipart_json_item(key=key, value=value)
request_field = self.__multipart_json_item(key=key, value=value)
return request_field

def __serialize_multipart_form_data(
self, in_data: Schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,11 @@ def test_upload_file(self):
name='file',
data=file_bytes,
filename=file_name,
headers={'Content-Type': 'application/octet-stream'}
headers={
'Content-Location': None,
'Content-Type': 'image/png',
"Content-Disposition": "form-data; name=\"file\"; filename=\"1px_pic1.png\""
}
),
),
content_type='multipart/form-data'
Expand All @@ -492,7 +496,11 @@ def test_upload_file(self):
api_client.RequestField(
name='file',
data=file_bytes,
headers={'Content-Type': 'application/octet-stream'}
headers={
'Content-Type': 'application/octet-stream',
"Content-Disposition": "form-data; name=\"file\"",
"Content-Location": None
}
),
),
content_type='multipart/form-data'
Expand Down Expand Up @@ -547,13 +555,22 @@ def test_upload_files(self):
name='files',
data=file_bytes,
filename=file_name,
headers={'Content-Type': 'application/octet-stream'}
headers={
'Content-Type': 'image/png',
"Content-Disposition": "form-data; name=\"files\"; filename=\"1px_pic1.png\"",
"Content-Location": None
}
),
api_client.RequestField(
name='files',
data=file_bytes,
filename=file_name,
headers={'Content-Type': 'application/octet-stream'}
headers={
'Content-Type': 'image/png',
"Content-Disposition": "form-data; name=\"files\"; filename=\"1px_pic1.png\"",
"Content-Location": None

}
),
),
content_type='multipart/form-data'
Expand All @@ -578,12 +595,20 @@ def test_upload_files(self):
api_client.RequestField(
name='files',
data=file_bytes,
headers={'Content-Type': 'application/octet-stream'}
headers={
'Content-Type': 'application/octet-stream',
"Content-Disposition": "form-data; name=\"files\"",
"Content-Location": None
}
),
api_client.RequestField(
name='files',
data=file_bytes,
headers={'Content-Type': 'application/octet-stream'}
headers={
'Content-Type': 'application/octet-stream',
"Content-Disposition": "form-data; name=\"files\"",
"Content-Location": None
}
),
),
content_type='multipart/form-data'
Expand Down Expand Up @@ -656,11 +681,15 @@ def test_inline_composition(self, mock_request):
accept_content_type=content_type,
content_type=content_type,
fields=(
api_client.RequestField(
name='someProp',
data=single_char_str,
headers={'Content-Type': 'text/plain'}
),
api_client.RequestField(
name='someProp',
data=single_char_str,
headers={
'Content-Type': 'text/plain',
"Content-Disposition": "form-data; name=\"someProp\"",
"Content-Location": None
}
),
),
)
self.assertEqual(api_response.body, {'someProp': single_char_str})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,53 @@ def test_content_multipart_form_data_serialization(self):
dict(
fields=(
api_client.RequestField(
name='some_null', data='null', headers={'Content-Type': 'application/json'}),
name='some_null', data='null', headers={
'Content-Type': 'application/json',
"Content-Disposition": "form-data; name=\"some_null\"",
"Content-Location": None
}),
api_client.RequestField(
name='some_bool', data='true', headers={'Content-Type': 'application/json'}),
name='some_bool', data='true', headers={
'Content-Type': 'application/json',
"Content-Disposition": "form-data; name=\"some_bool\"",
"Content-Location": None
}),
api_client.RequestField(
name='some_str', data='a', headers={'Content-Type': 'text/plain'}),
name='some_str', data='a', headers={
'Content-Type': 'text/plain',
"Content-Disposition": "form-data; name=\"some_str\"",
"Content-Location": None
}),
api_client.RequestField(
name='some_int', data='1', headers={'Content-Type': 'application/json'}),
name='some_int', data='1', headers={
'Content-Type': 'application/json',
"Content-Disposition": "form-data; name=\"some_int\"",
"Content-Location": None
}),
api_client.RequestField(
name='some_float', data='3.14', headers={'Content-Type': 'application/json'}),
name='some_float', data='3.14', headers={
'Content-Type': 'application/json',
"Content-Disposition": "form-data; name=\"some_float\"",
"Content-Location": None
}),
api_client.RequestField(
name='some_list', data='[]', headers={'Content-Type': 'application/json'}),
name='some_list', data='[]', headers={
'Content-Type': 'application/json',
"Content-Disposition": "form-data; name=\"some_list\"",
"Content-Location": None
}),
api_client.RequestField(
name='some_dict', data='{}', headers={'Content-Type': 'application/json'}),
name='some_dict', data='{}', headers={
'Content-Type': 'application/json',
"Content-Disposition": "form-data; name=\"some_dict\"",
"Content-Location": None
}),
api_client.RequestField(
name='some_bytes', data=b'abc', headers={'Content-Type': 'application/octet-stream'})
name='some_bytes', data=b'abc', headers={
'Content-Type': 'application/octet-stream',
"Content-Disposition": "form-data; name=\"some_bytes\"",
"Content-Location": None
})
)
)
)
Expand Down