Skip to content

Commit

Permalink
request.openapi_validated does not break non-opeanpi views
Browse files Browse the repository at this point in the history
Accessing `request.openapi_validated` in a route that has
`view_config(openapi=False)` will no longer break the route.

Refs #165
  • Loading branch information
zupo committed May 27, 2022
1 parent bbe71e8 commit b549975
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 14 additions & 3 deletions pyramid_openapi3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,24 @@ def openapi_validated(request: Request) -> dict:
"Cannot do openapi request validation on a view marked with openapi=False"
)

validate_request = asbool(
request.registry.settings.get(
"pyramid_openapi3.enable_request_validation", True
)
)
validate_response = asbool(
request.registry.settings.get(
"pyramid_openapi3.enable_response_validation", True
)
)
request.environ["pyramid_openapi3.validate_response"] = validate_response
gsettings = settings = request.registry.settings["pyramid_openapi3"]
route_settings = gsettings.get("routes")
if route_settings and request.matched_route.name in route_settings:
settings = request.registry.settings[route_settings[request.matched_route.name]]

if request.environ.get("pyramid_openapi3.validate_request"):
# if request.environ.get("pyramid_openapi3.validate_request"):
if validate_request:
openapi_request = PyramidOpenAPIRequestFactory.create(request)
validated = settings["request_validator"].validate(openapi_request)
return validated
Expand All @@ -104,8 +116,7 @@ def openapi_view(view: View, info: ViewDeriverInfo) -> View:
if info.options.get("openapi"):

def wrapper_view(context: Context, request: Request) -> Response:
__import__("pdb").set_trace()
# __import__("pdb").set_trace()

# We need this to be able to raise AttributeError if view code
# accesses request.openapi_validated on a view that is marked
# with openapi=False
Expand Down
12 changes: 3 additions & 9 deletions pyramid_openapi3/tests/test_path_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@
from webtest.app import TestApp


class _FooResource:
def __init__(self, request: Request) -> None:
self.request = request
self.foo_id = request.openapi_validated.parameters["path"]["foo_id"]


def _foo_view(context: _FooResource, request: Request) -> int:
return context.foo_id
def _foo_view(request: Request) -> int:
return request.openapi_validated.parameters["path"]["foo_id"]


def test_path_parameter_validation() -> None:
Expand Down Expand Up @@ -47,7 +41,7 @@ def test_path_parameter_validation() -> None:
config.pyramid_openapi3_spec(tempdoc.name)
config.pyramid_openapi3_register_routes()
# config.add_route("foo_route", "/foo/{foo_id}", factory=_FooResource)
config.add_route("foo_route", "/foo/{foo_id}", factory=_FooResource)
config.add_route("foo_route", "/foo/{foo_id}")
config.add_view(
openapi=True, view=_foo_view, route_name="foo_route", renderer="json"
)
Expand Down

0 comments on commit b549975

Please sign in to comment.