Skip to content

Commit 33d32cb

Browse files
committed
Add section on adding top-level components objects
Adapted from #222 (comment)
1 parent 24b2fc5 commit 33d32cb

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/special_topics.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,58 @@ JSON
4343
import json
4444
4545
json.dumps(spec.to_dict())
46+
47+
Documenting Top-level Components
48+
--------------------------------
49+
50+
To add objects within the top-level ``components`` object, pass the
51+
components to the ``APISpec`` as a keyword argument.
52+
53+
Here is an example that includes a `Security Scheme Object <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#securitySchemeObject>`_.
54+
55+
.. code-block:: python
56+
57+
import yaml
58+
from apispec import APISpec
59+
from apispec.utils import validate_swagger
60+
61+
OPENAPI_SPEC = """
62+
openapi: 3.0.1
63+
info:
64+
description: Server API document
65+
title: Server API
66+
version: 1.0.0
67+
servers:
68+
- url: http://localhost:{port}/
69+
description: The development API server
70+
variables:
71+
port:
72+
enum:
73+
- '3000'
74+
- '8888'
75+
default: '3000'
76+
components:
77+
securitySchemes:
78+
bearerAuth:
79+
type: http
80+
scheme: bearer
81+
bearerFormat: JWT
82+
"""
83+
84+
settings = yaml.load(OPENAPI_SPEC)
85+
# retrieve title, version, and openapi version
86+
title = settings['info'].pop('title')
87+
spec_version = settings['info'].pop('version')
88+
openapi_version = settings.pop('openapi')
89+
90+
spec = APISpec(
91+
title=title,
92+
version=spec_version,
93+
openapi_version=openapi_version,
94+
plugins=(
95+
'apispec.ext.marshmallow',
96+
),
97+
**settings
98+
)
99+
100+
validate_swagger(spec)

0 commit comments

Comments
 (0)