Skip to content
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
21 changes: 9 additions & 12 deletions openapi_core/contrib/falcon/handlers.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
"""OpenAPI core contrib falcon handlers module"""
from json import dumps

from falcon import status_codes
from falcon.constants import MEDIA_JSON
from falcon.status_codes import (
HTTP_400, HTTP_404, HTTP_405, HTTP_415,
)

from openapi_core.exceptions import MissingRequiredParameter
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
from openapi_core.templating.paths.exceptions import (
ServerNotFound, OperationNotFound, PathNotFound,
)
from openapi_core.validation.exceptions import InvalidSecurity


class FalconOpenAPIErrorsHandler:

OPENAPI_ERROR_STATUS = {
MissingRequiredParameter: 400,
ServerNotFound: 400,
InvalidSecurity: 403,
OperationNotFound: 405,
PathNotFound: 404,
MediaTypeNotFound: 415,
}

FALCON_STATUS_CODES = {
400: HTTP_400,
404: HTTP_404,
405: HTTP_405,
415: HTTP_415,
}

@classmethod
def handle(cls, req, resp, errors):
data_errors = [
Expand All @@ -40,8 +35,10 @@ def handle(cls, req, resp, errors):
data_str = dumps(data)
data_error_max = max(data_errors, key=cls.get_error_status)
resp.content_type = MEDIA_JSON
resp.status = cls.FALCON_STATUS_CODES.get(
data_error_max['status'], HTTP_400)
resp.status = getattr(
status_codes, f"HTTP_{data_error_max['status']}",
status_codes.HTTP_400,
)
resp.text = data_str
resp.complete = True

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from falcon import App

from falconproject.openapi import openapi_middleware
from falconproject.resources import PetListResource, PetDetailResource
from falconproject.pets.resources import PetListResource, PetDetailResource

app = App(middleware=[openapi_middleware])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import yaml

openapi_spec_path = Path("tests/integration/data/v3.0/petstore.yaml")
spec_yaml = openapi_spec_path.read_text()
spec_dict = yaml.load(spec_yaml)
spec_dict = yaml.load(openapi_spec_path.read_text(), yaml.Loader)
spec = create_spec(spec_dict)
openapi_middleware = FalconOpenAPIMiddleware.from_spec(spec)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from json import dumps

from falcon.constants import MEDIA_JSON
from falcon.status_codes import HTTP_200
from falcon.status_codes import HTTP_200, HTTP_201


class PetListResource:
Expand All @@ -27,6 +27,23 @@ def on_get(self, request, response):
response.text = dumps({"data": data})
response.set_header('X-Rate-Limit', '12')

def on_post(self, request, response):
assert request.openapi
assert not request.openapi.errors
assert request.openapi.parameters.cookie == {
'user': 1,
}
assert request.openapi.parameters.header == {
'api-key': '12345',
}
assert request.openapi.body.__class__.__name__ == 'PetCreate'
assert request.openapi.body.name == 'Cat'
assert request.openapi.body.ears.__class__.__name__ == 'Ears'
assert request.openapi.body.ears.healthy is True

response.status = HTTP_201
response.set_header('X-Rate-Limit', '12')


class PetDetailResource:
def on_get(self, request, response, petId=None):
Expand Down
60 changes: 0 additions & 60 deletions tests/integration/contrib/falcon/data/v3.0/openapi.yaml

This file was deleted.

67 changes: 0 additions & 67 deletions tests/integration/contrib/falcon/test_falcon.py

This file was deleted.

Loading