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