Skip to content
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
9 changes: 8 additions & 1 deletion openapi_core/schema/media_types/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ def generate(self, content):
for mimetype, media_type in iteritems(content):
schema_spec = media_type.get('schema')

example_spec = media_type.get('example')
example_type = type(example_spec)
if example_type is dict:
example = self.dereferencer.dereference(example_spec)
else:
example = example_spec

schema = None
if schema_spec:
schema, _ = self.schemas_registry.get_or_create(schema_spec)

yield mimetype, MediaType(mimetype, schema)
yield mimetype, MediaType(mimetype, schema=schema, example=example)
3 changes: 2 additions & 1 deletion openapi_core/schema/media_types/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
class MediaType(object):
"""Represents an OpenAPI MediaType."""

def __init__(self, mimetype, schema=None):
def __init__(self, mimetype, schema=None, example=None):
self.mimetype = mimetype
self.schema = schema
self.example = example

def get_deserializer_mapping(self):
mapping = MEDIA_TYPE_DESERIALIZERS.copy()
Expand Down
12 changes: 11 additions & 1 deletion tests/integration/data/v3.0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ paths:
schema:
type: string
content:
application/json:
application/json:
schema:
$ref: "#/components/schemas/PetsData"
post:
Expand All @@ -80,6 +80,9 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/PetCreate'
example:
name: "Pet"
wings: []
responses:
'201':
description: Null response
Expand All @@ -106,6 +109,10 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/PetData"
example: |
{
"data": []
}
image/*:
schema:
type: string
Expand All @@ -125,6 +132,9 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TagList"
example:
- dogs
- cats
default:
$ref: "#/components/responses/ErrorResponse"
post:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def test_spec(self, spec, spec_dict):
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)

Expand Down