Merge pull request #44 from p1c2u/feature/mimetype-wildcards-support

Mimetype wildcards support
This commit is contained in:
A 2018-05-30 15:08:30 +01:00 committed by GitHub
commit bb4bdc49d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 6 deletions

View file

View file

@ -0,0 +1,9 @@
from openapi_core.schema.exceptions import OpenAPIMappingError
class OpenAPIContentError(OpenAPIMappingError):
pass
class MimeTypeNotFound(OpenAPIContentError):
pass

View file

@ -0,0 +1,21 @@
"""OpenAPI core content models module"""
import fnmatch
from six import iteritems
from openapi_core.schema.content.exceptions import MimeTypeNotFound
class Content(dict):
def __getitem__(self, mimetype):
try:
return super(Content, self).__getitem__(mimetype)
except KeyError:
pass
for key, value in iteritems(self):
if fnmatch.fnmatch(mimetype, key):
return value
raise MimeTypeNotFound("{0} mimetype not found")

View file

@ -1,5 +1,6 @@
"""OpenAPI core request bodies models module"""
from openapi_core.schema.content.exceptions import MimeTypeNotFound
from openapi_core.schema.content.models import Content
from openapi_core.schema.media_types.exceptions import InvalidContentType
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
@ -8,13 +9,13 @@ class RequestBody(object):
"""Represents an OpenAPI RequestBody."""
def __init__(self, content, required=False):
self.content = dict(content)
self.content = Content(content)
self.required = required
def __getitem__(self, mimetype):
try:
return self.content[mimetype]
except KeyError:
except MimeTypeNotFound:
raise InvalidContentType(
"Invalid mime type `{0}`".format(mimetype))

View file

@ -1,4 +1,6 @@
"""OpenAPI core responses models module"""
from openapi_core.schema.content.exceptions import MimeTypeNotFound
from openapi_core.schema.content.models import Content
from openapi_core.schema.media_types.exceptions import InvalidContentType
from openapi_core.schema.responses.exceptions import MissingResponseContent
@ -11,13 +13,13 @@ class Response(object):
self.http_status = http_status
self.description = description
self.headers = headers and dict(headers) or {}
self.content = content and dict(content) or {}
self.content = content and Content(content) or Content()
self.links = links and dict(links) or {}
def __getitem__(self, mimetype):
try:
return self.content[mimetype]
except KeyError:
except MimeTypeNotFound:
raise InvalidContentType(
"Invalid mime type `{0}`".format(mimetype))

View file

@ -106,6 +106,10 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/PetData"
image/*:
schema:
type: string
format: binary
default:
$ref: "#/components/responses/ErrorResponse"
/tags:

View file

@ -112,7 +112,8 @@ class TestPetstore(object):
continue
assert type(media_type.schema) == Schema
assert media_type.schema.type == schema_spec['type']
assert media_type.schema.type.value ==\
schema_spec['type']
assert media_type.schema.required == schema_spec.get(
'required', [])
@ -675,6 +676,37 @@ class TestPetstore(object):
assert response_result.errors == []
assert response_result.data == data_json
def test_get_pet_wildcard(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets/{petId}'
view_args = {
'petId': '1',
}
request = MockRequest(
host_url, 'GET', '/pets/1',
path_pattern=path_pattern, view_args=view_args,
)
parameters = request.get_parameters(spec)
assert parameters == {
'path': {
'petId': 1,
}
}
body = request.get_body(spec)
assert body is None
data = b'imagedata'
response = MockResponse(data, mimetype='image/png')
response_result = response_validator.validate(request, response)
assert response_result.errors == []
assert response_result.data == data
def test_get_tags(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/tags'