Skip to content

Schema content refactor 2 #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions openapi_core/schema/responses/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from six import iteritems

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.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.responses.models import Response

Expand All @@ -20,30 +20,30 @@ def generate(self, responses):
response_deref = self.dereferencer.dereference(response)
description = response_deref['description']
headers = response_deref.get('headers')
content = response_deref.get('content')
content_spec = response_deref.get('content')
links_dict = response_deref.get('links', {})
links = self.links_generator.generate(links_dict)

extensions = self.extensions_generator.generate(response_deref)

media_types = None
if content:
media_types = self.media_types_generator.generate(content)
content = None
if content_spec:
content = self.content_factory.create(content_spec)

parameters = None
if headers:
parameters = self.parameters_generator.generate(headers)

yield http_status, Response(
http_status, description,
content=media_types, headers=parameters, links=links,
content=content, headers=parameters, links=links,
extensions=extensions,
)

@property
@lru_cache()
def media_types_generator(self):
return MediaTypeGenerator(self.dereferencer, self.schemas_registry)
def content_factory(self):
return ContentFactory(self.dereferencer, self.schemas_registry)

@property
@lru_cache()
Expand Down
3 changes: 1 addition & 2 deletions openapi_core/schema/responses/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""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


Expand All @@ -12,7 +11,7 @@ def __init__(
self.http_status = http_status
self.description = description
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.extensions = extensions and dict(extensions) or {}
Expand Down
55 changes: 30 additions & 25 deletions tests/integration/schema/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,31 +181,6 @@ def test_spec(self, spec, spec_dict):

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(
response.headers):
assert type(parameter) == Parameter
Expand Down Expand Up @@ -261,6 +236,36 @@ def test_spec(self, spec, spec_dict):
assert media_type.schema.required == \
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')

assert bool(request_body_spec) == bool(operation.request_body)
Expand Down