Skip to content

Bump falcon from 3.1.3 to 4.0.2 #967

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

Merged
merged 2 commits into from
Mar 22, 2025
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
4 changes: 4 additions & 0 deletions docs/integrations/falcon.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This section describes integration with [Falcon](https://falconframework.org) web framework.
The integration supports Falcon from version 3.0 and above.

!!! warning

This integration does not support multipart form body requests.

## Middleware

The Falcon API can be integrated by `FalconOpenAPIMiddleware` middleware.
Expand Down
4 changes: 1 addition & 3 deletions openapi_core/contrib/falcon/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def body(self) -> Optional[bytes]:
self.request.content_type, self.request.options.default_media_type
)
try:
body = handler.serialize(
media, content_type=self.request.content_type
)
body = handler.serialize(media, content_type=self.content_type)
# multipart form serialization is not supported
except NotImplementedError:
warnings.warn(
Expand Down
18 changes: 13 additions & 5 deletions openapi_core/contrib/falcon/responses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""OpenAPI core contrib falcon responses module"""

from io import BytesIO
from itertools import tee
from typing import Iterable

from falcon.response import Response
from werkzeug.datastructures import Headers
Expand All @@ -17,16 +19,22 @@ def data(self) -> bytes:
if self.response.text is None:
if self.response.stream is None:
return b""
resp_iter1, resp_iter2 = tee(self.response.stream)
self.response.stream = resp_iter1
content = b"".join(resp_iter2)
return content
if isinstance(self.response.stream, Iterable):
resp_iter1, resp_iter2 = tee(self.response.stream)
self.response.stream = resp_iter1
content = b"".join(resp_iter2)
return content
# checks ReadableIO protocol
if hasattr(self.response.stream, "read"):
data = self.response.stream.read()
self.response.stream = BytesIO(data)
return data
assert isinstance(self.response.text, str)
return self.response.text.encode("utf-8")

@property
def status_code(self) -> int:
return int(self.response.status[:3])
return self.response.status_code

@property
def content_type(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions openapi_core/contrib/falcon/util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any
from typing import Dict
from typing import Generator
from typing import Mapping
from typing import Tuple


def unpack_params(
params: Dict[str, Any]
params: Mapping[str, Any]
) -> Generator[Tuple[str, Any], None, None]:
for k, v in params.items():
if isinstance(v, list):
Expand Down
84 changes: 49 additions & 35 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/integration/contrib/django/test_django_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_post_media_type_invalid(self, client):
"title": (
"Content for the following mimetype not found: "
"text/html. "
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'text/plain']"
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain']"
),
}
]
Expand Down
40 changes: 39 additions & 1 deletion tests/integration/contrib/falcon/test_falcon_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from json import dumps

import pytest
from urllib3 import encode_multipart_formdata


class BaseTestFalconProject:
Expand Down Expand Up @@ -204,7 +205,7 @@ def test_post_media_type_invalid(self, client):
"title": (
"Content for the following mimetype not found: "
f"{content_type}. "
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'text/plain']"
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain']"
),
}
]
Expand Down Expand Up @@ -292,6 +293,43 @@ def test_post_valid(self, client, data_json):
assert response.status_code == 201
assert not response.content

@pytest.mark.xfail(
reason="falcon multipart form serialization unsupported",
strict=True,
)
def test_post_multipart_valid(self, client, data_gif):
cookies = {"user": 1}
auth = "authuser"
fields = {
"name": "Cat",
"address": (
"aaddress.json",
dumps(dict(city="Warsaw")),
"application/json",
),
"photo": (
"photo.jpg",
data_gif,
"image/jpeg",
),
}
body, content_type_header = encode_multipart_formdata(fields)
headers = {
"Authorization": f"Basic {auth}",
"Content-Type": content_type_header,
}

response = client.simulate_post(
"/v1/pets",
host="staging.gigantic-server.com",
headers=headers,
body=body,
cookies=cookies,
protocol="https",
)

assert response.status_code == 200


class TestPetDetailResource:
def test_get_server_invalid(self, client):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/contrib/fastapi/test_fastapi_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_post_media_type_invalid(self, client):
"title": (
"Content for the following mimetype not found: "
"text/html. "
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'text/plain']"
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain']"
),
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_post_media_type_invalid(self, client):
"title": (
"Content for the following mimetype not found: "
"text/html. "
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'text/plain']"
"Valid mimetypes: ['application/json', 'application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain']"
),
}
]
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/data/v3.0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ paths:
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/PetCreate'
multipart/form-data:
schema:
$ref: '#/components/schemas/PetWithPhotoCreate'
text/plain: {}
responses:
'201':
Expand Down Expand Up @@ -375,6 +378,16 @@ components:
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Bird"
PetWithPhotoCreate:
type: object
x-model: PetWithPhotoCreate
allOf:
- $ref: "#/components/schemas/PetCreatePartOne"
- $ref: "#/components/schemas/PetCreatePartTwo"
- $ref: "#/components/schemas/PetCreatePartPhoto"
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Bird"
PetCreatePartOne:
type: object
x-model: PetCreatePartOne
Expand All @@ -395,6 +408,15 @@ components:
$ref: "#/components/schemas/Position"
healthy:
type: boolean
PetCreatePartPhoto:
type: object
x-model: PetCreatePartPhoto
properties:
photo:
$ref: "#/components/schemas/PetPhoto"
PetPhoto:
type: string
format: binary
Bird:
type: object
x-model: Bird
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def test_invalid_content_type(self, request_unmarshaller):
availableMimetypes=[
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data",
"text/plain",
],
)
Expand Down
1 change: 1 addition & 0 deletions tests/integration/validation/test_request_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def test_media_type_not_found(self, request_validator):
availableMimetypes=[
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data",
"text/plain",
],
)
Expand Down
Loading