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""" """OpenAPI core contrib django requests module"""
import re import re
from six.moves.urllib.parse import urljoin
from openapi_core.validation.request.datatypes import ( from openapi_core.validation.request.datatypes import (
RequestParameters, OpenAPIRequest, RequestParameters, OpenAPIRequest,
) )
@ -40,11 +42,11 @@ class DjangoOpenAPIRequestFactory(object):
header=request.headers, header=request.headers,
cookie=request.COOKIES, cookie=request.COOKIES,
) )
full_url_pattern = urljoin(
request._current_scheme_host, path_pattern)
return OpenAPIRequest( return OpenAPIRequest(
host_url=request._current_scheme_host, full_url_pattern=full_url_pattern,
path=request.path,
method=method, method=method,
path_pattern=path_pattern,
parameters=parameters, parameters=parameters,
body=request.body, body=request.body,
mimetype=request.content_type, mimetype=request.content_type,

View file

@ -1,6 +1,8 @@
"""OpenAPI core contrib flask requests module""" """OpenAPI core contrib flask requests module"""
import re import re
from six.moves.urllib.parse import urljoin
from openapi_core.validation.request.datatypes import ( from openapi_core.validation.request.datatypes import (
RequestParameters, OpenAPIRequest, RequestParameters, OpenAPIRequest,
) )
@ -28,10 +30,9 @@ class FlaskOpenAPIRequestFactory(object):
header=request.headers, header=request.headers,
cookie=request.cookies, cookie=request.cookies,
) )
full_url_pattern = urljoin(request.host_url, path_pattern)
return OpenAPIRequest( return OpenAPIRequest(
host_url=request.host_url, full_url_pattern=full_url_pattern,
path=request.path,
path_pattern=path_pattern,
method=method, method=method,
parameters=parameters, parameters=parameters,
body=request.data, body=request.data,

View file

@ -1,4 +1,5 @@
"""OpenAPI core testing requests module""" """OpenAPI core testing requests module"""
from six.moves.urllib.parse import urljoin
from werkzeug.datastructures import ImmutableMultiDict from werkzeug.datastructures import ImmutableMultiDict
from openapi_core.validation.request.datatypes import ( from openapi_core.validation.request.datatypes import (
@ -22,10 +23,9 @@ class MockRequestFactory(object):
path_pattern = path_pattern or path path_pattern = path_pattern or path
method = method.lower() method = method.lower()
body = data or '' body = data or ''
full_url_pattern = urljoin(host_url, path_pattern)
return OpenAPIRequest( return OpenAPIRequest(
host_url=host_url, full_url_pattern=full_url_pattern,
path=path,
path_pattern=path_pattern,
method=method, method=method,
parameters=parameters, parameters=parameters,
body=body, body=body,

View file

@ -5,9 +5,6 @@ from werkzeug.datastructures import ImmutableMultiDict
from openapi_core.validation.datatypes import BaseValidationResult from openapi_core.validation.datatypes import BaseValidationResult
from six.moves.urllib.parse import urljoin
@attr.s @attr.s
class RequestParameters(object): class RequestParameters(object):
"""OpenAPI request parameters dataclass. """OpenAPI request parameters dataclass.
@ -36,10 +33,13 @@ class OpenAPIRequest(object):
"""OpenAPI request dataclass. """OpenAPI request dataclass.
Attributes: Attributes:
path full_url_pattern
Requested path as string. The matched url with scheme, host and path pattern.
path_pattern For example:
The matched url pattern. https://localhost:8000/api/v1/pets
https://localhost:8000/api/v1/pets/{pet_id}
method
The request method, as lowercase string.
parameters parameters
A RequestParameters object. A RequestParameters object.
body body
@ -51,21 +51,12 @@ class OpenAPIRequest(object):
the mimetype would be "text/html". the mimetype would be "text/html".
""" """
host_url = attr.ib() full_url_pattern = attr.ib()
path = attr.ib()
path_pattern = attr.ib()
method = attr.ib() method = attr.ib()
body = attr.ib() body = attr.ib()
mimetype = attr.ib() mimetype = attr.ib()
parameters = attr.ib(factory=RequestParameters) parameters = attr.ib(factory=RequestParameters)
@property
def full_url_pattern(self):
return urljoin(self.host_url, self.path_pattern)
@attr.s @attr.s
class RequestValidationResult(BaseValidationResult): class RequestValidationResult(BaseValidationResult):

View file

@ -1,3 +1,4 @@
from six.moves.urllib.parse import urljoin
from werkzeug.datastructures import EnvironHeaders, ImmutableMultiDict from werkzeug.datastructures import EnvironHeaders, ImmutableMultiDict
from openapi_core.contrib.flask import FlaskOpenAPIRequest from openapi_core.contrib.flask import FlaskOpenAPIRequest
@ -21,10 +22,9 @@ class TestFlaskOpenAPIRequest(object):
header=headers, header=headers,
cookie=cookies, 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.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.body == request.data
assert openapi_request.mimetype == request.mimetype assert openapi_request.mimetype == request.mimetype
@ -46,10 +46,9 @@ class TestFlaskOpenAPIRequest(object):
header=headers, header=headers,
cookie=cookies, 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.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.body == request.data
assert openapi_request.mimetype == request.mimetype assert openapi_request.mimetype == request.mimetype
@ -68,9 +67,8 @@ class TestFlaskOpenAPIRequest(object):
header=headers, header=headers,
cookie=cookies, 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.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.body == request.data
assert openapi_request.mimetype == request.mimetype assert openapi_request.mimetype == request.mimetype

View file

@ -81,10 +81,9 @@ class TestDjangoOpenAPIRequest(BaseTestDjango):
header=headers, header=headers,
cookie=cookies, 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.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.body == request.body
assert openapi_request.mimetype == request.content_type assert openapi_request.mimetype == request.content_type
@ -107,10 +106,9 @@ class TestDjangoOpenAPIRequest(BaseTestDjango):
header=headers, header=headers,
cookie=cookies, 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.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.body == request.body
assert openapi_request.mimetype == request.content_type assert openapi_request.mimetype == request.content_type
@ -135,11 +133,9 @@ class TestDjangoOpenAPIRequest(BaseTestDjango):
header=headers, header=headers,
cookie=cookies, 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.method == request.method.lower()
assert openapi_request.path_pattern == \ assert openapi_request.full_url_pattern == \
"/admin/auth/group/{object_id}/" request._current_scheme_host + "/admin/auth/group/{object_id}/"
assert openapi_request.body == request.body assert openapi_request.body == request.body
assert openapi_request.mimetype == request.content_type assert openapi_request.mimetype == request.content_type