Skip to content

OpenAPI request datatype refactor #197

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 1 commit into from
Feb 16, 2020
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
8 changes: 5 additions & 3 deletions openapi_core/contrib/django/requests.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down Expand Up @@ -40,11 +42,11 @@ def create(cls, request):
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,
Expand Down
7 changes: 4 additions & 3 deletions openapi_core/contrib/flask/requests.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down Expand Up @@ -28,10 +30,9 @@ def create(cls, request):
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,
Expand Down
6 changes: 3 additions & 3 deletions openapi_core/testing/requests.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -22,10 +23,9 @@ def create(
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,
Expand Down
25 changes: 8 additions & 17 deletions openapi_core/validation/request/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from openapi_core.validation.datatypes import BaseValidationResult


from six.moves.urllib.parse import urljoin


@attr.s
class RequestParameters(object):
"""OpenAPI request parameters dataclass.
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
16 changes: 7 additions & 9 deletions tests/integration/contrib/flask/test_flask_requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from six.moves.urllib.parse import urljoin
from werkzeug.datastructures import EnvironHeaders, ImmutableMultiDict

from openapi_core.contrib.flask import FlaskOpenAPIRequest
Expand All @@ -21,10 +22,9 @@ def test_simple(self, request_factory, request):
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

Expand All @@ -46,10 +46,9 @@ def test_multiple_values(self, request_factory, request):
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

Expand All @@ -68,9 +67,8 @@ def test_url_rule(self, request_factory, request):
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
16 changes: 6 additions & 10 deletions tests/integration/contrib/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ def test_no_resolver(self, request_factory):
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

Expand All @@ -107,10 +106,9 @@ def test_simple(self, request_factory):
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

Expand All @@ -135,11 +133,9 @@ def test_url_rule(self, request_factory):
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

Expand Down