From c900f63e7dfea12ae089945fb266a9986805885c Mon Sep 17 00:00:00 2001 From: p1c2u Date: Tue, 30 Mar 2021 12:29:37 +0100 Subject: [PATCH] Schema content refactor 2 --- openapi_core/schema/responses/generators.py | 16 +++--- openapi_core/schema/responses/models.py | 3 +- tests/integration/schema/test_spec.py | 55 +++++++++++---------- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/openapi_core/schema/responses/generators.py b/openapi_core/schema/responses/generators.py index f74e152..019235f 100644 --- a/openapi_core/schema/responses/generators.py +++ b/openapi_core/schema/responses/generators.py @@ -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 @@ -20,15 +20,15 @@ class ResponsesGenerator(object): 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: @@ -36,14 +36,14 @@ class ResponsesGenerator(object): 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() diff --git a/openapi_core/schema/responses/models.py b/openapi_core/schema/responses/models.py index 21dffc5..c6ae437 100644 --- a/openapi_core/schema/responses/models.py +++ b/openapi_core/schema/responses/models.py @@ -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 @@ -12,7 +11,7 @@ class Response(object): 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 {} diff --git a/tests/integration/schema/test_spec.py b/tests/integration/schema/test_spec.py index 6e208bc..e9e5f8a 100644 --- a/tests/integration/schema/test_spec.py +++ b/tests/integration/schema/test_spec.py @@ -181,31 +181,6 @@ class TestPetstore(object): 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 @@ -261,6 +236,36 @@ class TestPetstore(object): 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)