OpenAPI request datatype refactor

This commit is contained in:
Artur Maciag 2020-02-11 14:54:36 +00:00
parent 281aab89a8
commit e0d61c8f11
6 changed files with 33 additions and 45 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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):

View file

@ -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

View file

@ -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