| 1 |
******* |
| 2 |
apispec |
| 3 |
******* |
| 4 |
|
| 5 |
|pypi| |build-status| |docs| |marshmallow-support| |openapi| |
| 6 |
|
| 7 |
.. |pypi| image:: https://badgen.net/pypi/v/apispec |
| 8 |
:target: https://pypi.org/project/apispec/ |
| 9 |
:alt: PyPI package |
| 10 |
|
| 11 |
.. |build-status| image:: https://github.com/marshmallow-code/apispec/actions/workflows/build-release.yml/badge.svg |
| 12 |
:target: https://github.com/marshmallow-code/webargs/actions/workflows/build-release.yml |
| 13 |
:alt: Build status |
| 14 |
|
| 15 |
.. |docs| image:: https://readthedocs.org/projects/apispec/badge/ |
| 16 |
:target: https://apispec.readthedocs.io/ |
| 17 |
:alt: Documentation |
| 18 |
|
| 19 |
.. |marshmallow-support| image:: https://badgen.net/badge/marshmallow/3,4?list=1 |
| 20 |
:target: https://marshmallow.readthedocs.io/en/latest/upgrading.html |
| 21 |
:alt: marshmallow 3|4 compatible |
| 22 |
|
| 23 |
.. |openapi| image:: https://badgen.net/badge/OAS/2,3?list=1&color=cyan |
| 24 |
:target: https://github.com/OAI/OpenAPI-Specification |
| 25 |
:alt: OpenAPI Specification 2/3 compatible |
| 26 |
|
| 27 |
|
| 28 |
A pluggable API specification generator. Currently supports the `OpenAPI Specification <https://github.com/OAI/OpenAPI-Specification>`_ (f.k.a. the Swagger specification). |
| 29 |
|
| 30 |
Features |
| 31 |
======== |
| 32 |
|
| 33 |
- Supports the OpenAPI Specification (versions 2 and 3) |
| 34 |
- Framework-agnostic |
| 35 |
- Built-in support for `marshmallow <https://marshmallow.readthedocs.io/>`_ |
| 36 |
- Utilities for parsing docstrings |
| 37 |
|
| 38 |
Installation |
| 39 |
============ |
| 40 |
|
| 41 |
:: |
| 42 |
|
| 43 |
$ pip install -U apispec |
| 44 |
|
| 45 |
When using the marshmallow plugin, ensure a compatible marshmallow version is used: :: |
| 46 |
|
| 47 |
$ pip install -U apispec[marshmallow] |
| 48 |
|
| 49 |
Example Application |
| 50 |
=================== |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
from apispec import APISpec |
| 55 |
from apispec.ext.marshmallow import MarshmallowPlugin |
| 56 |
from apispec_webframeworks.flask import FlaskPlugin |
| 57 |
from flask import Flask |
| 58 |
from marshmallow import Schema, fields |
| 59 |
|
| 60 |
|
| 61 |
# Create an APISpec |
| 62 |
spec = APISpec( |
| 63 |
title="Swagger Petstore", |
| 64 |
version="1.0.0", |
| 65 |
openapi_version="3.0.2", |
| 66 |
plugins=[FlaskPlugin(), MarshmallowPlugin()], |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
# Optional marshmallow support |
| 71 |
class CategorySchema(Schema): |
| 72 |
id = fields.Int() |
| 73 |
name = fields.Str(required=True) |
| 74 |
|
| 75 |
|
| 76 |
class PetSchema(Schema): |
| 77 |
category = fields.List(fields.Nested(CategorySchema)) |
| 78 |
name = fields.Str() |
| 79 |
|
| 80 |
|
| 81 |
# Optional security scheme support |
| 82 |
api_key_scheme = {"type": "apiKey", "in": "header", "name": "X-API-Key"} |
| 83 |
spec.components.security_scheme("ApiKeyAuth", api_key_scheme) |
| 84 |
|
| 85 |
|
| 86 |
# Optional Flask support |
| 87 |
app = Flask(__name__) |
| 88 |
|
| 89 |
|
| 90 |
@app.route("/random") |
| 91 |
def random_pet(): |
| 92 |
"""A cute furry animal endpoint. |
| 93 |
--- |
| 94 |
get: |
| 95 |
description: Get a random pet |
| 96 |
security: |
| 97 |
- ApiKeyAuth: [] |
| 98 |
responses: |
| 99 |
200: |
| 100 |
content: |
| 101 |
application/json: |
| 102 |
schema: PetSchema |
| 103 |
""" |
| 104 |
pet = get_random_pet() |
| 105 |
return PetSchema().dump(pet) |
| 106 |
|
| 107 |
|
| 108 |
# Register the path and the entities within it |
| 109 |
with app.test_request_context(): |
| 110 |
spec.path(view=random_pet) |
| 111 |
|
| 112 |
|
| 113 |
Generated OpenAPI Spec |
| 114 |
---------------------- |
| 115 |
|
| 116 |
.. code-block:: python |
| 117 |
|
| 118 |
import json |
| 119 |
|
| 120 |
print(json.dumps(spec.to_dict(), indent=2)) |
| 121 |
# { |
| 122 |
# "paths": { |
| 123 |
# "/random": { |
| 124 |
# "get": { |
| 125 |
# "description": "Get a random pet", |
| 126 |
# "security": [ |
| 127 |
# { |
| 128 |
# "ApiKeyAuth": [] |
| 129 |
# } |
| 130 |
# ], |
| 131 |
# "responses": { |
| 132 |
# "200": { |
| 133 |
# "content": { |
| 134 |
# "application/json": { |
| 135 |
# "schema": { |
| 136 |
# "$ref": "#/components/schemas/Pet" |
| 137 |
# } |
| 138 |
# } |
| 139 |
# } |
| 140 |
# } |
| 141 |
# } |
| 142 |
# } |
| 143 |
# } |
| 144 |
# }, |
| 145 |
# "tags": [], |
| 146 |
# "info": { |
| 147 |
# "title": "Swagger Petstore", |
| 148 |
# "version": "1.0.0" |
| 149 |
# }, |
| 150 |
# "openapi": "3.0.2", |
| 151 |
# "components": { |
| 152 |
# "parameters": {}, |
| 153 |
# "responses": {}, |
| 154 |
# "schemas": { |
| 155 |
# "Category": { |
| 156 |
# "type": "object", |
| 157 |
# "properties": { |
| 158 |
# "name": { |
| 159 |
# "type": "string" |
| 160 |
# }, |
| 161 |
# "id": { |
| 162 |
# "type": "integer", |
| 163 |
# "format": "int32" |
| 164 |
# } |
| 165 |
# }, |
| 166 |
# "required": [ |
| 167 |
# "name" |
| 168 |
# ] |
| 169 |
# }, |
| 170 |
# "Pet": { |
| 171 |
# "type": "object", |
| 172 |
# "properties": { |
| 173 |
# "name": { |
| 174 |
# "type": "string" |
| 175 |
# }, |
| 176 |
# "category": { |
| 177 |
# "type": "array", |
| 178 |
# "items": { |
| 179 |
# "$ref": "#/components/schemas/Category" |
| 180 |
# } |
| 181 |
# } |
| 182 |
# } |
| 183 |
# } |
| 184 |
# "securitySchemes": { |
| 185 |
# "ApiKeyAuth": { |
| 186 |
# "type": "apiKey", |
| 187 |
# "in": "header", |
| 188 |
# "name": "X-API-Key" |
| 189 |
# } |
| 190 |
# } |
| 191 |
# } |
| 192 |
# } |
| 193 |
# } |
| 194 |
|
| 195 |
print(spec.to_yaml()) |
| 196 |
# components: |
| 197 |
# parameters: {} |
| 198 |
# responses: {} |
| 199 |
# schemas: |
| 200 |
# Category: |
| 201 |
# properties: |
| 202 |
# id: {format: int32, type: integer} |
| 203 |
# name: {type: string} |
| 204 |
# required: [name] |
| 205 |
# type: object |
| 206 |
# Pet: |
| 207 |
# properties: |
| 208 |
# category: |
| 209 |
# items: {$ref: '#/components/schemas/Category'} |
| 210 |
# type: array |
| 211 |
# name: {type: string} |
| 212 |
# type: object |
| 213 |
# securitySchemes: |
| 214 |
# ApiKeyAuth: |
| 215 |
# in: header |
| 216 |
# name: X-API-KEY |
| 217 |
# type: apiKey |
| 218 |
# info: {title: Swagger Petstore, version: 1.0.0} |
| 219 |
# openapi: 3.0.2 |
| 220 |
# paths: |
| 221 |
# /random: |
| 222 |
# get: |
| 223 |
# description: Get a random pet |
| 224 |
# responses: |
| 225 |
# 200: |
| 226 |
# content: |
| 227 |
# application/json: |
| 228 |
# schema: {$ref: '#/components/schemas/Pet'} |
| 229 |
# security: |
| 230 |
# - ApiKeyAuth: [] |
| 231 |
# tags: [] |
| 232 |
|
| 233 |
|
| 234 |
Documentation |
| 235 |
============= |
| 236 |
|
| 237 |
Documentation is available at https://apispec.readthedocs.io/ . |
| 238 |
|
| 239 |
Ecosystem |
| 240 |
========= |
| 241 |
|
| 242 |
A list of apispec-related libraries can be found at the GitHub wiki here: |
| 243 |
|
| 244 |
https://github.com/marshmallow-code/apispec/wiki/Ecosystem |
| 245 |
|
| 246 |
Support apispec |
| 247 |
=============== |
| 248 |
|
| 249 |
apispec is maintained by a group of |
| 250 |
`volunteers <https://apispec.readthedocs.io/en/latest/authors.html>`_. |
| 251 |
If you'd like to support the future of the project, please consider |
| 252 |
contributing to our Open Collective: |
| 253 |
|
| 254 |
.. image:: https://opencollective.com/marshmallow/donate/button.png |
| 255 |
:target: https://opencollective.com/marshmallow |
| 256 |
:width: 200 |
| 257 |
:alt: Donate to our collective |
| 258 |
|
| 259 |
Professional Support |
| 260 |
==================== |
| 261 |
|
| 262 |
Professionally-supported apispec is available through the |
| 263 |
`Tidelift Subscription <https://tidelift.com/subscription/pkg/pypi-apispec?utm_source=pypi-apispec&utm_medium=referral&utm_campaign=readme>`_. |
| 264 |
|
| 265 |
Tidelift gives software development teams a single source for purchasing and maintaining their software, |
| 266 |
with professional-grade assurances from the experts who know it best, |
| 267 |
while seamlessly integrating with existing tools. [`Get professional support`_] |
| 268 |
|
| 269 |
.. _`Get professional support`: https://tidelift.com/subscription/pkg/pypi-apispec?utm_source=pypi-apispec&utm_medium=referral&utm_campaign=readme |
| 270 |
|
| 271 |
.. image:: https://user-images.githubusercontent.com/2379650/45126032-50b69880-b13f-11e8-9c2c-abd16c433495.png |
| 272 |
:target: https://tidelift.com/subscription/pkg/pypi-apispec?utm_source=pypi-apispec&utm_medium=referral&utm_campaign=readme |
| 273 |
:alt: Get supported apispec with Tidelift |
| 274 |
|
| 275 |
Security Contact Information |
| 276 |
============================ |
| 277 |
|
| 278 |
To report a security vulnerability, please use the |
| 279 |
`Tidelift security contact <https://tidelift.com/security>`_. |
| 280 |
Tidelift will coordinate the fix and disclosure. |
| 281 |
|
| 282 |
Project Links |
| 283 |
============= |
| 284 |
|
| 285 |
- Docs: https://apispec.readthedocs.io/ |
| 286 |
- Changelog: https://apispec.readthedocs.io/en/latest/changelog.html |
| 287 |
- Contributing Guidelines: https://apispec.readthedocs.io/en/latest/contributing.html |
| 288 |
- PyPI: https://pypi.python.org/pypi/apispec |
| 289 |
- Issues: https://github.com/marshmallow-code/apispec/issues |
| 290 |
|
| 291 |
|
| 292 |
License |
| 293 |
======= |
| 294 |
|
| 295 |
MIT licensed. See the bundled `LICENSE <https://github.com/marshmallow-code/apispec/blob/dev/LICENSE>`_ file for more details. |