| 1 |
""" |
| 2 |
In this example `openapi` version is used instead of `swagger` version. |
| 3 |
""" |
| 4 |
from flask import Flask |
| 5 |
from flasgger import Swagger |
| 6 |
|
| 7 |
app = Flask(__name__) |
| 8 |
|
| 9 |
swagger_config = { |
| 10 |
"headers": [], |
| 11 |
"openapi": "3.0.2", |
| 12 |
"components": { |
| 13 |
"securitySchemes": { |
| 14 |
"oAuthSample": { |
| 15 |
"type": "oauth2", |
| 16 |
"flows": { |
| 17 |
"clientCredentials": { |
| 18 |
"tokenUrl": "https://api.pgsmartshopassistant.com/o/token/", |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
}, |
| 23 |
}, |
| 24 |
"servers": [ |
| 25 |
{ |
| 26 |
"url": "https://api.example.com/v1", |
| 27 |
"description": "Production server (uses live data)" |
| 28 |
}, |
| 29 |
{ |
| 30 |
"url": "https://sandbox-api.example.com:8443/v1", |
| 31 |
"description": "Sandbox server (uses test data)" |
| 32 |
} |
| 33 |
], |
| 34 |
"specs": [ |
| 35 |
{ |
| 36 |
"endpoint": "swagger", |
| 37 |
"route": "/characteristics/swagger.json", |
| 38 |
"rule_filter": lambda rule: True, |
| 39 |
"model_filter": lambda tag: True, |
| 40 |
} |
| 41 |
], |
| 42 |
"title": "Product Characteristics API", |
| 43 |
"version": '', |
| 44 |
"termsOfService": "", |
| 45 |
"static_url_path": "/characteristics/static", |
| 46 |
"swagger_ui": True, |
| 47 |
"specs_route": "/characteristics/swagger/", |
| 48 |
"description": "", |
| 49 |
} |
| 50 |
|
| 51 |
colors_spec = { |
| 52 |
"tags": [ |
| 53 |
"colors" |
| 54 |
], |
| 55 |
"parameters": [ |
| 56 |
{ |
| 57 |
"name": "palette", |
| 58 |
"in": "path", |
| 59 |
"type": "string", |
| 60 |
"enum": [ |
| 61 |
"all", |
| 62 |
"rgb", |
| 63 |
"cmyk" |
| 64 |
], |
| 65 |
"required": True, |
| 66 |
"default": "all", |
| 67 |
"description": "Which palette to filter?" |
| 68 |
} |
| 69 |
], |
| 70 |
"operationId": "get_colors", |
| 71 |
"consumes": [ |
| 72 |
"application/json" |
| 73 |
], |
| 74 |
"produces": [ |
| 75 |
"application/json" |
| 76 |
], |
| 77 |
"security": { |
| 78 |
"colors_auth": [ |
| 79 |
"write:colors", |
| 80 |
"read:colors" |
| 81 |
] |
| 82 |
}, |
| 83 |
"schemes": [ |
| 84 |
"http", |
| 85 |
"https" |
| 86 |
], |
| 87 |
"deprecated": False, |
| 88 |
"externalDocs": { |
| 89 |
"description": "Project repository", |
| 90 |
"url": "http://github.com/rochacbruno/flasgger" |
| 91 |
}, |
| 92 |
"definitions": { |
| 93 |
"Palette": { |
| 94 |
"type": "object", |
| 95 |
"properties": { |
| 96 |
"palette_name": { |
| 97 |
"type": "array", |
| 98 |
"items": { |
| 99 |
"$ref": "#/definitions/Color" |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
}, |
| 104 |
"Color": { |
| 105 |
"type": "string" |
| 106 |
} |
| 107 |
}, |
| 108 |
"responses": { |
| 109 |
"200": { |
| 110 |
"description": "A list of colors (may be filtered by palette)", |
| 111 |
"schema": { |
| 112 |
"$ref": "#/definitions/Palette" |
| 113 |
}, |
| 114 |
"examples": { |
| 115 |
"rgb": [ |
| 116 |
"red", |
| 117 |
"green", |
| 118 |
"blue" |
| 119 |
] |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
swag = Swagger(app, config=swagger_config) |
| 127 |
|
| 128 |
|
| 129 |
def test_swag(client, specs_data): |
| 130 |
""" |
| 131 |
This test is runs automatically in Travis CI |
| 132 |
|
| 133 |
:param client: Flask app test client |
| 134 |
:param specs_data: {'url': {swag_specs}} for every spec in app |
| 135 |
""" |
| 136 |
for spec in specs_data.values(): |
| 137 |
assert 'openapi' in spec |
| 138 |
assert '3.0.2' == spec['openapi'] |
| 139 |
assert 'swagger' not in spec |
| 140 |
assert 'components' in spec |
| 141 |
assert 'securitySchemes' in spec['components'] |
| 142 |
assert 'oAuthSample' in spec['components']['securitySchemes'] |
| 143 |
|
| 144 |
assert 'servers' in spec |
| 145 |
|
| 146 |
|
| 147 |
if __name__ == '__main__': |
| 148 |
app.run(debug=True) |