Skip to content

Commit

Permalink
Added example support
Browse files Browse the repository at this point in the history
Added tests
Completes a marshmallow-code#245 Todo
  • Loading branch information
codeasashu committed Nov 3, 2019
1 parent 30df5f2 commit bd07ab7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ Contributors (chronological)
- Dave `@zedrdave <https://github.com/zedrdave>`_
- Emmanuel Valette `@karec <https://github.com/karec/>`_
- Hugo van Kemenade `@hugovk <https://github.com/hugovk>`_
- Ashutosh Chaudhary `@codeasashu <https://github.com/codeasashu>`_
18 changes: 18 additions & 0 deletions src/apispec/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ def __init__(self, plugins, openapi_version):
self._plugins = plugins
self.openapi_version = openapi_version
self._schemas = {}
self._examples = {}
self._parameters = {}
self._responses = {}
self._security_schemes = {}

def to_dict(self):
subsections = {
"example": self._examples,
"schema": self._schemas,
"parameter": self._parameters,
"response": self._responses,
Expand All @@ -47,6 +49,22 @@ def to_dict(self):
if v != {}
}

def example(self, name, component=None, **kwargs):
"""Add a new example to the spec
:param str name: identifier by which example may be referenced.
:param dict component: schema definition.
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#exampleObject
"""
if name in self._examples:
raise DuplicateComponentNameError(
'Another example with name "{}" is already registered.'.format(name)
)
# Skipping plugin helper
self._examples[name] = component or {}
return self

def schema(self, name, component=None, **kwargs):
"""Add a new schema to the spec.
Expand Down
1 change: 1 addition & 0 deletions src/apispec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"security_scheme": "securityDefinitions",
},
3: {
"example": "examples",
"schema": "schemas",
"parameter": "parameters",
"response": "responses",
Expand Down
9 changes: 9 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .utils import (
get_schemas,
get_examples,
get_paths,
get_parameters,
get_responses,
Expand Down Expand Up @@ -137,6 +138,14 @@ class TestComponents:
"name": {"type": "string", "example": "doggie"},
}

@pytest.mark.parametrize("spec", ("3.0.0",), indirect=True)
def test_example(self, spec):
# Referenced examples are only supported in OAS 3.x
spec.components.example("PetExample", {"value": {"a": "b"}})
defs = get_examples(spec)
assert "PetExample" in defs
assert defs["PetExample"]["value"] == {"a": "b"}

def test_schema(self, spec):
spec.components.schema("Pet", {"properties": self.properties})
defs = get_schemas(spec)
Expand Down
6 changes: 6 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from apispec.utils import build_reference


def get_examples(spec):
if spec.openapi_version.major < 3:
return spec.to_dict()["definitions"]
return spec.to_dict()["components"]["examples"]


def get_schemas(spec):
if spec.openapi_version.major < 3:
return spec.to_dict()["definitions"]
Expand Down

0 comments on commit bd07ab7

Please sign in to comment.