Skip to content

fix: Fix multipart body file array #938

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions end_to_end_tests/baseline_openapi_3.0.json
Original file line number Diff line number Diff line change
@@ -87,6 +87,57 @@
}
}
},
"/bodies/multipart/multiple-files-in-object": {
"post": {
"tags": [
"tests"
],
"summary": "Array of files in object",
"description": "Upload an array of files as part of an object",
"operationId": "upload_array_of_files_in_object_tests_upload_post",
"parameters": [],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type" : "object",
"properties" : {
"files": {
"type": "array",
"items": {
"type": "string",
"description": "attachments content",
"format": "binary"
}
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/tests/": {
"get": {
"tags": [
51 changes: 51 additions & 0 deletions end_to_end_tests/baseline_openapi_3.1.yaml
Original file line number Diff line number Diff line change
@@ -83,6 +83,57 @@ info:
}
}
},
"/bodies/multipart/multiple-files-in-object": {
"post": {
"tags": [
"tests"
],
"summary": "Array of files in object",
"description": "Upload an array of files as part of an object",
"operationId": "upload_array_of_files_in_object_tests_upload_post",
"parameters": [ ],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"files": {
"type": "array",
"items": {
"type": "string",
"description": "attachments content",
"format": "binary"
}
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": { }
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
},
"/tests/": {
"get": {
"tags": [
Original file line number Diff line number Diff line change
@@ -20,12 +20,20 @@
test_inline_objects,
token_with_cookie_auth_token_with_cookie_get,
unsupported_content_tests_unsupported_content_get,
upload_array_of_files_in_object_tests_upload_post,
upload_file_tests_upload_post,
upload_multiple_files_tests_upload_post,
)


class TestsEndpoints:
@classmethod
def upload_array_of_files_in_object_tests_upload_post(cls) -> types.ModuleType:
"""
Upload an array of files as part of an object
"""
return upload_array_of_files_in_object_tests_upload_post

@classmethod
def get_user_list(cls) -> types.ModuleType:
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
from http import HTTPStatus
from typing import Any, Dict, Optional, Union

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.http_validation_error import HTTPValidationError
from ...models.upload_array_of_files_in_object_tests_upload_post_body import (
UploadArrayOfFilesInObjectTestsUploadPostBody,
)
from ...types import Response


def _get_kwargs(
*,
body: UploadArrayOfFilesInObjectTestsUploadPostBody,
) -> Dict[str, Any]:
headers: Dict[str, Any] = {}

_kwargs: Dict[str, Any] = {
"method": "post",
"url": "/bodies/multipart/multiple-files-in-object",
}

_body = body.to_multipart()

_kwargs["files"] = _body

_kwargs["headers"] = headers
return _kwargs


def _parse_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Optional[Union[Any, HTTPValidationError]]:
if response.status_code == HTTPStatus.OK:
response_200 = response.json()
return response_200
if response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY:
response_422 = HTTPValidationError.from_dict(response.json())

return response_422
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Response[Union[Any, HTTPValidationError]]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: UploadArrayOfFilesInObjectTestsUploadPostBody,
) -> Response[Union[Any, HTTPValidationError]]:
"""Array of files in object

Upload an array of files as part of an object

Args:
body (UploadArrayOfFilesInObjectTestsUploadPostBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Union[Any, HTTPValidationError]]
"""

kwargs = _get_kwargs(
body=body,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


def sync(
*,
client: Union[AuthenticatedClient, Client],
body: UploadArrayOfFilesInObjectTestsUploadPostBody,
) -> Optional[Union[Any, HTTPValidationError]]:
"""Array of files in object

Upload an array of files as part of an object

Args:
body (UploadArrayOfFilesInObjectTestsUploadPostBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Union[Any, HTTPValidationError]
"""

return sync_detailed(
client=client,
body=body,
).parsed


async def asyncio_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: UploadArrayOfFilesInObjectTestsUploadPostBody,
) -> Response[Union[Any, HTTPValidationError]]:
"""Array of files in object

Upload an array of files as part of an object

Args:
body (UploadArrayOfFilesInObjectTestsUploadPostBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Union[Any, HTTPValidationError]]
"""

kwargs = _get_kwargs(
body=body,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)


async def asyncio(
*,
client: Union[AuthenticatedClient, Client],
body: UploadArrayOfFilesInObjectTestsUploadPostBody,
) -> Optional[Union[Any, HTTPValidationError]]:
"""Array of files in object

Upload an array of files as part of an object

Args:
body (UploadArrayOfFilesInObjectTestsUploadPostBody):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Union[Any, HTTPValidationError]
"""

return (
await asyncio_detailed(
client=client,
body=body,
)
).parsed
Original file line number Diff line number Diff line change
@@ -80,6 +80,7 @@
)
from .test_inline_objects_body import TestInlineObjectsBody
from .test_inline_objects_response_200 import TestInlineObjectsResponse200
from .upload_array_of_files_in_object_tests_upload_post_body import UploadArrayOfFilesInObjectTestsUploadPostBody
from .validation_error import ValidationError

__all__ = (
@@ -153,5 +154,6 @@
"PostResponsesUnionsSimpleBeforeComplexResponse200AType1",
"TestInlineObjectsBody",
"TestInlineObjectsResponse200",
"UploadArrayOfFilesInObjectTestsUploadPostBody",
"ValidationError",
)
Original file line number Diff line number Diff line change
@@ -186,6 +186,7 @@ def to_dict(self) -> Dict[str, Any]:
not_required_nullable_model = self.not_required_nullable_model

field_dict: Dict[str, Any] = {}

field_dict.update(
{
"an_enum_value": an_enum_value,
Loading