mirror of
https://github.com/correl/openapi-core.git
synced 2025-01-01 11:03:19 +00:00
Merge pull request #44 from p1c2u/feature/mimetype-wildcards-support
Mimetype wildcards support
This commit is contained in:
commit
bb4bdc49d2
7 changed files with 75 additions and 6 deletions
0
openapi_core/schema/content/__init__.py
Normal file
0
openapi_core/schema/content/__init__.py
Normal file
9
openapi_core/schema/content/exceptions.py
Normal file
9
openapi_core/schema/content/exceptions.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
from openapi_core.schema.exceptions import OpenAPIMappingError
|
||||||
|
|
||||||
|
|
||||||
|
class OpenAPIContentError(OpenAPIMappingError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MimeTypeNotFound(OpenAPIContentError):
|
||||||
|
pass
|
21
openapi_core/schema/content/models.py
Normal file
21
openapi_core/schema/content/models.py
Normal 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")
|
|
@ -1,5 +1,6 @@
|
||||||
"""OpenAPI core request bodies models module"""
|
"""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.media_types.exceptions import InvalidContentType
|
||||||
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
|
from openapi_core.schema.request_bodies.exceptions import MissingRequestBody
|
||||||
|
|
||||||
|
@ -8,13 +9,13 @@ class RequestBody(object):
|
||||||
"""Represents an OpenAPI RequestBody."""
|
"""Represents an OpenAPI RequestBody."""
|
||||||
|
|
||||||
def __init__(self, content, required=False):
|
def __init__(self, content, required=False):
|
||||||
self.content = dict(content)
|
self.content = Content(content)
|
||||||
self.required = required
|
self.required = required
|
||||||
|
|
||||||
def __getitem__(self, mimetype):
|
def __getitem__(self, mimetype):
|
||||||
try:
|
try:
|
||||||
return self.content[mimetype]
|
return self.content[mimetype]
|
||||||
except KeyError:
|
except MimeTypeNotFound:
|
||||||
raise InvalidContentType(
|
raise InvalidContentType(
|
||||||
"Invalid mime type `{0}`".format(mimetype))
|
"Invalid mime type `{0}`".format(mimetype))
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
"""OpenAPI core responses models module"""
|
"""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.media_types.exceptions import InvalidContentType
|
||||||
from openapi_core.schema.responses.exceptions import MissingResponseContent
|
from openapi_core.schema.responses.exceptions import MissingResponseContent
|
||||||
|
|
||||||
|
@ -11,13 +13,13 @@ class Response(object):
|
||||||
self.http_status = http_status
|
self.http_status = http_status
|
||||||
self.description = description
|
self.description = description
|
||||||
self.headers = headers and dict(headers) or {}
|
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 {}
|
self.links = links and dict(links) or {}
|
||||||
|
|
||||||
def __getitem__(self, mimetype):
|
def __getitem__(self, mimetype):
|
||||||
try:
|
try:
|
||||||
return self.content[mimetype]
|
return self.content[mimetype]
|
||||||
except KeyError:
|
except MimeTypeNotFound:
|
||||||
raise InvalidContentType(
|
raise InvalidContentType(
|
||||||
"Invalid mime type `{0}`".format(mimetype))
|
"Invalid mime type `{0}`".format(mimetype))
|
||||||
|
|
||||||
|
|
|
@ -106,6 +106,10 @@ paths:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/PetData"
|
$ref: "#/components/schemas/PetData"
|
||||||
|
image/*:
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
format: binary
|
||||||
default:
|
default:
|
||||||
$ref: "#/components/responses/ErrorResponse"
|
$ref: "#/components/responses/ErrorResponse"
|
||||||
/tags:
|
/tags:
|
||||||
|
|
|
@ -112,7 +112,8 @@ class TestPetstore(object):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
assert type(media_type.schema) == Schema
|
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(
|
assert media_type.schema.required == schema_spec.get(
|
||||||
'required', [])
|
'required', [])
|
||||||
|
|
||||||
|
@ -675,6 +676,37 @@ class TestPetstore(object):
|
||||||
assert response_result.errors == []
|
assert response_result.errors == []
|
||||||
assert response_result.data == data_json
|
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):
|
def test_get_tags(self, spec, response_validator):
|
||||||
host_url = 'http://petstore.swagger.io/v1'
|
host_url = 'http://petstore.swagger.io/v1'
|
||||||
path_pattern = '/v1/tags'
|
path_pattern = '/v1/tags'
|
||||||
|
|
Loading…
Reference in a new issue