Skip to content

Commit 1747433

Browse files
authored
Merge pull request #306 from p1c2u/refactor/schema-content-refactor-2
Schema content refactor 2
2 parents f6efa84 + c900f63 commit 1747433

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

Diff for: openapi_core/schema/responses/generators.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from six import iteritems
33

44
from openapi_core.compat import lru_cache
5+
from openapi_core.schema.content.factories import ContentFactory
56
from openapi_core.schema.extensions.generators import ExtensionsGenerator
67
from openapi_core.schema.links.generators import LinksGenerator
7-
from openapi_core.schema.media_types.generators import MediaTypeGenerator
88
from openapi_core.schema.parameters.generators import ParametersGenerator
99
from openapi_core.schema.responses.models import Response
1010

@@ -20,30 +20,30 @@ def generate(self, responses):
2020
response_deref = self.dereferencer.dereference(response)
2121
description = response_deref['description']
2222
headers = response_deref.get('headers')
23-
content = response_deref.get('content')
23+
content_spec = response_deref.get('content')
2424
links_dict = response_deref.get('links', {})
2525
links = self.links_generator.generate(links_dict)
2626

2727
extensions = self.extensions_generator.generate(response_deref)
2828

29-
media_types = None
30-
if content:
31-
media_types = self.media_types_generator.generate(content)
29+
content = None
30+
if content_spec:
31+
content = self.content_factory.create(content_spec)
3232

3333
parameters = None
3434
if headers:
3535
parameters = self.parameters_generator.generate(headers)
3636

3737
yield http_status, Response(
3838
http_status, description,
39-
content=media_types, headers=parameters, links=links,
39+
content=content, headers=parameters, links=links,
4040
extensions=extensions,
4141
)
4242

4343
@property
4444
@lru_cache()
45-
def media_types_generator(self):
46-
return MediaTypeGenerator(self.dereferencer, self.schemas_registry)
45+
def content_factory(self):
46+
return ContentFactory(self.dereferencer, self.schemas_registry)
4747

4848
@property
4949
@lru_cache()

Diff for: openapi_core/schema/responses/models.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""OpenAPI core responses models module"""
22
from openapi_core.schema.content.exceptions import MimeTypeNotFound
3-
from openapi_core.schema.content.models import Content
43
from openapi_core.schema.media_types.exceptions import InvalidContentType
54

65

@@ -12,7 +11,7 @@ def __init__(
1211
self.http_status = http_status
1312
self.description = description
1413
self.headers = headers and dict(headers) or {}
15-
self.content = content and Content(content) or Content()
14+
self.content = content
1615
self.links = links and dict(links) or {}
1716

1817
self.extensions = extensions and dict(extensions) or {}

Diff for: tests/integration/schema/test_spec.py

+30-25
Original file line numberDiff line numberDiff line change
@@ -181,31 +181,6 @@ def test_spec(self, spec, spec_dict):
181181

182182
assert response.description == description_spec
183183

184-
for mimetype, media_type in iteritems(response.content):
185-
assert type(media_type) == MediaType
186-
assert media_type.mimetype == mimetype
187-
188-
content_spec = response_spec['content'][mimetype]
189-
190-
example_spec = content_spec.get('example')
191-
assert media_type.example == example_spec
192-
193-
schema_spec = content_spec.get('schema')
194-
assert bool(schema_spec) == bool(media_type.schema)
195-
196-
if not schema_spec:
197-
continue
198-
199-
# @todo: test with defererence
200-
if '$ref' in schema_spec:
201-
continue
202-
203-
assert type(media_type.schema) == Schema
204-
assert media_type.schema.type.value ==\
205-
schema_spec['type']
206-
assert media_type.schema.required == schema_spec.get(
207-
'required', [])
208-
209184
for parameter_name, parameter in iteritems(
210185
response.headers):
211186
assert type(parameter) == Parameter
@@ -261,6 +236,36 @@ def test_spec(self, spec, spec_dict):
261236
assert media_type.schema.required == \
262237
schema_spec.get('required', False)
263238

239+
content_spec = response_spec.get('content')
240+
241+
if not content_spec:
242+
continue
243+
244+
for mimetype, media_type in iteritems(response.content):
245+
assert type(media_type) == MediaType
246+
assert media_type.mimetype == mimetype
247+
248+
content_spec = response_spec['content'][mimetype]
249+
250+
example_spec = content_spec.get('example')
251+
assert media_type.example == example_spec
252+
253+
schema_spec = content_spec.get('schema')
254+
assert bool(schema_spec) == bool(media_type.schema)
255+
256+
if not schema_spec:
257+
continue
258+
259+
# @todo: test with defererence
260+
if '$ref' in schema_spec:
261+
continue
262+
263+
assert type(media_type.schema) == Schema
264+
assert media_type.schema.type.value ==\
265+
schema_spec['type']
266+
assert media_type.schema.required == schema_spec.get(
267+
'required', [])
268+
264269
request_body_spec = operation_spec.get('requestBody')
265270

266271
assert bool(request_body_spec) == bool(operation.request_body)

0 commit comments

Comments
 (0)