1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
*******
apispec
*******
Release v\ |version| (:doc:`Changelog <changelog>`)
A pluggable API specification generator. Currently supports the `OpenAPI Specification <https://github.com/OAI/OpenAPI-Specification>`_ (f.k.a. the Swagger specification).
Features
========
- Supports the OpenAPI Specification (versions 2 and 3)
- Framework-agnostic
- Built-in support for `marshmallow <https://marshmallow.readthedocs.io/>`_
- Utilities for parsing docstrings
Example Application
===================
.. code-block:: python
import uuid
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from flask import Flask
from marshmallow import Schema, fields
# Create an APISpec
spec = APISpec(
title="Swagger Petstore",
version="1.0.0",
openapi_version="3.0.2",
plugins=[FlaskPlugin(), MarshmallowPlugin()],
)
# Optional marshmallow support
class CategorySchema(Schema):
id = fields.Int()
name = fields.Str(required=True)
class PetSchema(Schema):
categories = fields.List(fields.Nested(CategorySchema))
name = fields.Str()
# Optional security scheme support
api_key_scheme = {"type": "apiKey", "in": "header", "name": "X-API-Key"}
spec.components.security_scheme("ApiKeyAuth", api_key_scheme)
# Optional Flask support
app = Flask(__name__)
@app.route("/random")
def random_pet():
"""A cute furry animal endpoint.
---
get:
description: Get a random pet
security:
- ApiKeyAuth: []
responses:
200:
description: Return a pet
content:
application/json:
schema: PetSchema
"""
# Hardcoded example data
pet_data = {
"name": "sample_pet_" + str(uuid.uuid1()),
"categories": [{"id": 1, "name": "sample_category"}],
}
return PetSchema().dump(pet_data)
# Register the path and the entities within it
with app.test_request_context():
spec.path(view=random_pet)
Generated OpenAPI Spec
----------------------
.. code-block:: python
import json
print(json.dumps(spec.to_dict(), indent=2))
# {
# "info": {
# "title": "Swagger Petstore",
# "version": "1.0.0"
# },
# "openapi": "3.0.2",
# "components": {
# "schemas": {
# "Category": {
# "type": "object",
# "properties": {
# "id": {
# "type": "integer",
# "format": "int32"
# },
# "name": {
# "type": "string"
# }
# },
# "required": [
# "name"
# ]
# },
# "Pet": {
# "type": "object",
# "properties": {
# "categories": {
# "type": "array",
# "items": {
# "$ref": "#/components/schemas/Category"
# }
# },
# "name": {
# "type": "string"
# }
# }
# },
# }
# },
# "securitySchemes": {
# "ApiKeyAuth": {
# "type": "apiKey",
# "in": "header",
# "name": "X-API-Key"
# }
# },
# "paths": {
# "/random": {
# "get": {
# "description": "Get a random pet",
# "security": [
# {
# "ApiKeyAuth": []
# }
# ],
# "responses": {
# "200": {
# "description": "Return a pet",
# "content": {
# "application/json": {
# "schema": {
# "$ref": "#/components/schemas/Pet"
# }
# }
# }
# }
# }
# }
# }
# },
# }
print(spec.to_yaml())
# info:
# title: Swagger Petstore
# version: 1.0.0
# openapi: 3.0.2
# components:
# schemas:
# Category:
# properties:
# id:
# format: int32
# type: integer
# name:
# type: string
# required:
# - name
# type: object
# Pet:
# properties:
# categories:
# items:
# $ref: '#/components/schemas/Category'
# type: array
# name:
# type: string
# type: object
# securitySchemes:
# ApiKeyAuth:
# in: header
# name: X-API-KEY
# type: apiKey
# paths:
# /random:
# get:
# description: Get a random pet
# responses:
# '200':
# content:
# application/json:
# schema:
# $ref: '#/components/schemas/Pet'
# description: Return a pet
# security:
# - ApiKeyAuth: []
User Guide
==========
.. toctree::
:maxdepth: 2
install
quickstart
using_plugins
writing_plugins
special_topics
API Reference
=============
.. toctree::
:maxdepth: 2
api_core
api_ext
Project Links
=============
- `apispec @ GitHub <https://github.com/marshmallow-code/apispec>`_
- `Issue Tracker <https://github.com/marshmallow-code/apispec/issues>`_
Project Info
============
.. toctree::
:maxdepth: 1
changelog
upgrading
ecosystem
authors
contributing
license
|