Merge pull request #63 from rafaelcaricio/parse-examples

Makes it possible to access API examples
This commit is contained in:
A 2018-07-28 11:24:23 +01:00 committed by GitHub
commit db37be8ac7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 3 deletions

View file

@ -14,8 +14,15 @@ class MediaTypeGenerator(object):
for mimetype, media_type in iteritems(content): for mimetype, media_type in iteritems(content):
schema_spec = media_type.get('schema') 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 schema = None
if schema_spec: if schema_spec:
schema, _ = self.schemas_registry.get_or_create(schema_spec) schema, _ = self.schemas_registry.get_or_create(schema_spec)
yield mimetype, MediaType(mimetype, schema) yield mimetype, MediaType(mimetype, schema=schema, example=example)

View file

@ -15,9 +15,10 @@ MEDIA_TYPE_DESERIALIZERS = {
class MediaType(object): class MediaType(object):
"""Represents an OpenAPI MediaType.""" """Represents an OpenAPI MediaType."""
def __init__(self, mimetype, schema=None): def __init__(self, mimetype, schema=None, example=None):
self.mimetype = mimetype self.mimetype = mimetype
self.schema = schema self.schema = schema
self.example = example
def get_deserializer_mapping(self): def get_deserializer_mapping(self):
mapping = MEDIA_TYPE_DESERIALIZERS.copy() mapping = MEDIA_TYPE_DESERIALIZERS.copy()

View file

@ -66,7 +66,7 @@ paths:
schema: schema:
type: string type: string
content: content:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/PetsData" $ref: "#/components/schemas/PetsData"
post: post:
@ -80,6 +80,9 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/PetCreate' $ref: '#/components/schemas/PetCreate'
example:
name: "Pet"
wings: []
responses: responses:
'201': '201':
description: Null response description: Null response
@ -106,6 +109,10 @@ paths:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/PetData" $ref: "#/components/schemas/PetData"
example: |
{
"data": []
}
image/*: image/*:
schema: schema:
type: string type: string
@ -125,6 +132,9 @@ paths:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/TagList" $ref: "#/components/schemas/TagList"
example:
- dogs
- cats
default: default:
$ref: "#/components/responses/ErrorResponse" $ref: "#/components/responses/ErrorResponse"
post: post:

View file

@ -102,6 +102,10 @@ class TestPetstore(object):
assert media_type.mimetype == mimetype assert media_type.mimetype == mimetype
content_spec = response_spec['content'][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') schema_spec = content_spec.get('schema')
assert bool(schema_spec) == bool(media_type.schema) assert bool(schema_spec) == bool(media_type.schema)