From 15fc9b009ae1b4512ba063de66fd9826616335e4 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Tue, 24 Jul 2018 09:47:02 +0200 Subject: [PATCH] Makes it possible to access API examples --- openapi_core/schema/media_types/generators.py | 9 ++++++++- openapi_core/schema/media_types/models.py | 3 ++- tests/integration/data/v3.0/petstore.yaml | 12 +++++++++++- tests/integration/test_petstore.py | 4 ++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/openapi_core/schema/media_types/generators.py b/openapi_core/schema/media_types/generators.py index 1db858d..3eceffb 100644 --- a/openapi_core/schema/media_types/generators.py +++ b/openapi_core/schema/media_types/generators.py @@ -14,8 +14,15 @@ class MediaTypeGenerator(object): 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) diff --git a/openapi_core/schema/media_types/models.py b/openapi_core/schema/media_types/models.py index 194d005..e1a170a 100644 --- a/openapi_core/schema/media_types/models.py +++ b/openapi_core/schema/media_types/models.py @@ -15,9 +15,10 @@ MEDIA_TYPE_DESERIALIZERS = { 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() diff --git a/tests/integration/data/v3.0/petstore.yaml b/tests/integration/data/v3.0/petstore.yaml index 4f6c807..d4b5907 100644 --- a/tests/integration/data/v3.0/petstore.yaml +++ b/tests/integration/data/v3.0/petstore.yaml @@ -66,7 +66,7 @@ paths: schema: type: string content: - application/json: + application/json: schema: $ref: "#/components/schemas/PetsData" post: @@ -80,6 +80,9 @@ paths: application/json: schema: $ref: '#/components/schemas/PetCreate' + example: + name: "Pet" + wings: [] responses: '201': description: Null response @@ -106,6 +109,10 @@ paths: application/json: schema: $ref: "#/components/schemas/PetData" + example: | + { + "data": [] + } image/*: schema: type: string @@ -125,6 +132,9 @@ paths: application/json: schema: $ref: "#/components/schemas/TagList" + example: + - dogs + - cats default: $ref: "#/components/responses/ErrorResponse" post: diff --git a/tests/integration/test_petstore.py b/tests/integration/test_petstore.py index 17738b4..0f3bbb7 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/test_petstore.py @@ -102,6 +102,10 @@ class TestPetstore(object): 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)