diff --git a/openapi_core/contrib/django/requests.py b/openapi_core/contrib/django/requests.py index 5ed31f6..72653d8 100644 --- a/openapi_core/contrib/django/requests.py +++ b/openapi_core/contrib/django/requests.py @@ -1,6 +1,8 @@ """OpenAPI core contrib django requests module""" import re +from six.moves.urllib.parse import urljoin + from openapi_core.validation.request.datatypes import ( RequestParameters, OpenAPIRequest, ) @@ -40,11 +42,11 @@ class DjangoOpenAPIRequestFactory(object): header=request.headers, cookie=request.COOKIES, ) + full_url_pattern = urljoin( + request._current_scheme_host, path_pattern) return OpenAPIRequest( - host_url=request._current_scheme_host, - path=request.path, + full_url_pattern=full_url_pattern, method=method, - path_pattern=path_pattern, parameters=parameters, body=request.body, mimetype=request.content_type, diff --git a/openapi_core/contrib/flask/requests.py b/openapi_core/contrib/flask/requests.py index 93e6538..d9b5d26 100644 --- a/openapi_core/contrib/flask/requests.py +++ b/openapi_core/contrib/flask/requests.py @@ -1,6 +1,8 @@ """OpenAPI core contrib flask requests module""" import re +from six.moves.urllib.parse import urljoin + from openapi_core.validation.request.datatypes import ( RequestParameters, OpenAPIRequest, ) @@ -28,10 +30,9 @@ class FlaskOpenAPIRequestFactory(object): header=request.headers, cookie=request.cookies, ) + full_url_pattern = urljoin(request.host_url, path_pattern) return OpenAPIRequest( - host_url=request.host_url, - path=request.path, - path_pattern=path_pattern, + full_url_pattern=full_url_pattern, method=method, parameters=parameters, body=request.data, diff --git a/openapi_core/testing/requests.py b/openapi_core/testing/requests.py index d2a1b85..7d3d0ed 100644 --- a/openapi_core/testing/requests.py +++ b/openapi_core/testing/requests.py @@ -1,4 +1,5 @@ """OpenAPI core testing requests module""" +from six.moves.urllib.parse import urljoin from werkzeug.datastructures import ImmutableMultiDict from openapi_core.validation.request.datatypes import ( @@ -22,10 +23,9 @@ class MockRequestFactory(object): path_pattern = path_pattern or path method = method.lower() body = data or '' + full_url_pattern = urljoin(host_url, path_pattern) return OpenAPIRequest( - host_url=host_url, - path=path, - path_pattern=path_pattern, + full_url_pattern=full_url_pattern, method=method, parameters=parameters, body=body, diff --git a/openapi_core/validation/request/datatypes.py b/openapi_core/validation/request/datatypes.py index 9356000..abece0a 100644 --- a/openapi_core/validation/request/datatypes.py +++ b/openapi_core/validation/request/datatypes.py @@ -5,9 +5,6 @@ from werkzeug.datastructures import ImmutableMultiDict from openapi_core.validation.datatypes import BaseValidationResult -from six.moves.urllib.parse import urljoin - - @attr.s class RequestParameters(object): """OpenAPI request parameters dataclass. @@ -36,10 +33,13 @@ class OpenAPIRequest(object): """OpenAPI request dataclass. Attributes: - path - Requested path as string. - path_pattern - The matched url pattern. + full_url_pattern + The matched url with scheme, host and path pattern. + For example: + https://localhost:8000/api/v1/pets + https://localhost:8000/api/v1/pets/{pet_id} + method + The request method, as lowercase string. parameters A RequestParameters object. body @@ -51,21 +51,12 @@ class OpenAPIRequest(object): the mimetype would be "text/html". """ - host_url = attr.ib() - path = attr.ib() - path_pattern = attr.ib() + full_url_pattern = attr.ib() method = attr.ib() - body = attr.ib() - mimetype = attr.ib() - parameters = attr.ib(factory=RequestParameters) - @property - def full_url_pattern(self): - return urljoin(self.host_url, self.path_pattern) - @attr.s class RequestValidationResult(BaseValidationResult): diff --git a/tests/integration/contrib/flask/test_flask_requests.py b/tests/integration/contrib/flask/test_flask_requests.py index 86f844b..613f316 100644 --- a/tests/integration/contrib/flask/test_flask_requests.py +++ b/tests/integration/contrib/flask/test_flask_requests.py @@ -1,3 +1,4 @@ +from six.moves.urllib.parse import urljoin from werkzeug.datastructures import EnvironHeaders, ImmutableMultiDict from openapi_core.contrib.flask import FlaskOpenAPIRequest @@ -21,10 +22,9 @@ class TestFlaskOpenAPIRequest(object): header=headers, cookie=cookies, ) - assert openapi_request.host_url == request.host_url - assert openapi_request.path == request.path assert openapi_request.method == request.method.lower() - assert openapi_request.path_pattern == request.path + assert openapi_request.full_url_pattern == \ + urljoin(request.host_url, request.path) assert openapi_request.body == request.data assert openapi_request.mimetype == request.mimetype @@ -46,10 +46,9 @@ class TestFlaskOpenAPIRequest(object): header=headers, cookie=cookies, ) - assert openapi_request.host_url == request.host_url - assert openapi_request.path == request.path assert openapi_request.method == request.method.lower() - assert openapi_request.path_pattern == request.path + assert openapi_request.full_url_pattern == \ + urljoin(request.host_url, request.path) assert openapi_request.body == request.data assert openapi_request.mimetype == request.mimetype @@ -68,9 +67,8 @@ class TestFlaskOpenAPIRequest(object): header=headers, cookie=cookies, ) - assert openapi_request.host_url == request.host_url - assert openapi_request.path == request.path assert openapi_request.method == request.method.lower() - assert openapi_request.path_pattern == '/browse/{id}/' + assert openapi_request.full_url_pattern == \ + urljoin(request.host_url, '/browse/{id}/') assert openapi_request.body == request.data assert openapi_request.mimetype == request.mimetype diff --git a/tests/integration/contrib/test_django.py b/tests/integration/contrib/test_django.py index fd57984..7d60879 100644 --- a/tests/integration/contrib/test_django.py +++ b/tests/integration/contrib/test_django.py @@ -81,10 +81,9 @@ class TestDjangoOpenAPIRequest(BaseTestDjango): header=headers, cookie=cookies, ) - assert openapi_request.host_url == request._current_scheme_host - assert openapi_request.path == request.path assert openapi_request.method == request.method.lower() - assert openapi_request.path_pattern == request.path + assert openapi_request.full_url_pattern == \ + request._current_scheme_host + request.path assert openapi_request.body == request.body assert openapi_request.mimetype == request.content_type @@ -107,10 +106,9 @@ class TestDjangoOpenAPIRequest(BaseTestDjango): header=headers, cookie=cookies, ) - assert openapi_request.host_url == request._current_scheme_host - assert openapi_request.path == request.path assert openapi_request.method == request.method.lower() - assert openapi_request.path_pattern == request.path + assert openapi_request.full_url_pattern == \ + request._current_scheme_host + request.path assert openapi_request.body == request.body assert openapi_request.mimetype == request.content_type @@ -135,11 +133,9 @@ class TestDjangoOpenAPIRequest(BaseTestDjango): header=headers, cookie=cookies, ) - assert openapi_request.host_url == request._current_scheme_host - assert openapi_request.path == request.path assert openapi_request.method == request.method.lower() - assert openapi_request.path_pattern == \ - "/admin/auth/group/{object_id}/" + assert openapi_request.full_url_pattern == \ + request._current_scheme_host + "/admin/auth/group/{object_id}/" assert openapi_request.body == request.body assert openapi_request.mimetype == request.content_type