Skip to content

Commit db37be8

Browse files
authored
Merge pull request #63 from rafaelcaricio/parse-examples
Makes it possible to access API examples
2 parents 5ffcc6a + 15fc9b0 commit db37be8

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

openapi_core/schema/media_types/generators.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ def generate(self, content):
1414
for mimetype, media_type in iteritems(content):
1515
schema_spec = media_type.get('schema')
1616

17+
example_spec = media_type.get('example')
18+
example_type = type(example_spec)
19+
if example_type is dict:
20+
example = self.dereferencer.dereference(example_spec)
21+
else:
22+
example = example_spec
23+
1724
schema = None
1825
if schema_spec:
1926
schema, _ = self.schemas_registry.get_or_create(schema_spec)
2027

21-
yield mimetype, MediaType(mimetype, schema)
28+
yield mimetype, MediaType(mimetype, schema=schema, example=example)

openapi_core/schema/media_types/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
class MediaType(object):
1616
"""Represents an OpenAPI MediaType."""
1717

18-
def __init__(self, mimetype, schema=None):
18+
def __init__(self, mimetype, schema=None, example=None):
1919
self.mimetype = mimetype
2020
self.schema = schema
21+
self.example = example
2122

2223
def get_deserializer_mapping(self):
2324
mapping = MEDIA_TYPE_DESERIALIZERS.copy()

tests/integration/data/v3.0/petstore.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ paths:
6666
schema:
6767
type: string
6868
content:
69-
application/json:
69+
application/json:
7070
schema:
7171
$ref: "#/components/schemas/PetsData"
7272
post:
@@ -80,6 +80,9 @@ paths:
8080
application/json:
8181
schema:
8282
$ref: '#/components/schemas/PetCreate'
83+
example:
84+
name: "Pet"
85+
wings: []
8386
responses:
8487
'201':
8588
description: Null response
@@ -106,6 +109,10 @@ paths:
106109
application/json:
107110
schema:
108111
$ref: "#/components/schemas/PetData"
112+
example: |
113+
{
114+
"data": []
115+
}
109116
image/*:
110117
schema:
111118
type: string
@@ -125,6 +132,9 @@ paths:
125132
application/json:
126133
schema:
127134
$ref: "#/components/schemas/TagList"
135+
example:
136+
- dogs
137+
- cats
128138
default:
129139
$ref: "#/components/responses/ErrorResponse"
130140
post:

tests/integration/test_petstore.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ def test_spec(self, spec, spec_dict):
102102
assert media_type.mimetype == mimetype
103103

104104
content_spec = response_spec['content'][mimetype]
105+
106+
example_spec = content_spec.get('example')
107+
assert media_type.example == example_spec
108+
105109
schema_spec = content_spec.get('schema')
106110
assert bool(schema_spec) == bool(media_type.schema)
107111

0 commit comments

Comments
 (0)