Merge pull request #306 from p1c2u/refactor/schema-content-refactor-2

Schema content refactor 2
This commit is contained in:
A 2021-03-30 12:39:53 +01:00 committed by GitHub
commit 1747433ded
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 35 deletions

View file

@ -2,9 +2,9 @@
from six import iteritems from six import iteritems
from openapi_core.compat import lru_cache from openapi_core.compat import lru_cache
from openapi_core.schema.content.factories import ContentFactory
from openapi_core.schema.extensions.generators import ExtensionsGenerator from openapi_core.schema.extensions.generators import ExtensionsGenerator
from openapi_core.schema.links.generators import LinksGenerator from openapi_core.schema.links.generators import LinksGenerator
from openapi_core.schema.media_types.generators import MediaTypeGenerator
from openapi_core.schema.parameters.generators import ParametersGenerator from openapi_core.schema.parameters.generators import ParametersGenerator
from openapi_core.schema.responses.models import Response from openapi_core.schema.responses.models import Response
@ -20,15 +20,15 @@ class ResponsesGenerator(object):
response_deref = self.dereferencer.dereference(response) response_deref = self.dereferencer.dereference(response)
description = response_deref['description'] description = response_deref['description']
headers = response_deref.get('headers') headers = response_deref.get('headers')
content = response_deref.get('content') content_spec = response_deref.get('content')
links_dict = response_deref.get('links', {}) links_dict = response_deref.get('links', {})
links = self.links_generator.generate(links_dict) links = self.links_generator.generate(links_dict)
extensions = self.extensions_generator.generate(response_deref) extensions = self.extensions_generator.generate(response_deref)
media_types = None content = None
if content: if content_spec:
media_types = self.media_types_generator.generate(content) content = self.content_factory.create(content_spec)
parameters = None parameters = None
if headers: if headers:
@ -36,14 +36,14 @@ class ResponsesGenerator(object):
yield http_status, Response( yield http_status, Response(
http_status, description, http_status, description,
content=media_types, headers=parameters, links=links, content=content, headers=parameters, links=links,
extensions=extensions, extensions=extensions,
) )
@property @property
@lru_cache() @lru_cache()
def media_types_generator(self): def content_factory(self):
return MediaTypeGenerator(self.dereferencer, self.schemas_registry) return ContentFactory(self.dereferencer, self.schemas_registry)
@property @property
@lru_cache() @lru_cache()

View file

@ -1,6 +1,5 @@
"""OpenAPI core responses models module""" """OpenAPI core responses models module"""
from openapi_core.schema.content.exceptions import MimeTypeNotFound 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
@ -12,7 +11,7 @@ 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 Content(content) or Content() self.content = content
self.links = links and dict(links) or {} self.links = links and dict(links) or {}
self.extensions = extensions and dict(extensions) or {} self.extensions = extensions and dict(extensions) or {}

View file

@ -181,31 +181,6 @@ class TestPetstore(object):
assert response.description == description_spec assert response.description == description_spec
for mimetype, media_type in iteritems(response.content):
assert type(media_type) == MediaType
assert media_type.mimetype == mimetype
content_spec = response_spec['content'][mimetype]
example_spec = content_spec.get('example')
assert media_type.example == example_spec
schema_spec = content_spec.get('schema')
assert bool(schema_spec) == bool(media_type.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(media_type.schema) == Schema
assert media_type.schema.type.value ==\
schema_spec['type']
assert media_type.schema.required == schema_spec.get(
'required', [])
for parameter_name, parameter in iteritems( for parameter_name, parameter in iteritems(
response.headers): response.headers):
assert type(parameter) == Parameter assert type(parameter) == Parameter
@ -261,6 +236,36 @@ class TestPetstore(object):
assert media_type.schema.required == \ assert media_type.schema.required == \
schema_spec.get('required', False) schema_spec.get('required', False)
content_spec = response_spec.get('content')
if not content_spec:
continue
for mimetype, media_type in iteritems(response.content):
assert type(media_type) == MediaType
assert media_type.mimetype == mimetype
content_spec = response_spec['content'][mimetype]
example_spec = content_spec.get('example')
assert media_type.example == example_spec
schema_spec = content_spec.get('schema')
assert bool(schema_spec) == bool(media_type.schema)
if not schema_spec:
continue
# @todo: test with defererence
if '$ref' in schema_spec:
continue
assert type(media_type.schema) == Schema
assert media_type.schema.type.value ==\
schema_spec['type']
assert media_type.schema.required == schema_spec.get(
'required', [])
request_body_spec = operation_spec.get('requestBody') request_body_spec = operation_spec.get('requestBody')
assert bool(request_body_spec) == bool(operation.request_body) assert bool(request_body_spec) == bool(operation.request_body)