Debian Codegraph @

examples/swagger_config_3_with_components_schemas.py

raw symbols SCIP
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, # all in
39 "model_filter": lambda tag: True, # all in
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 "components":{
93 "schemas":{
94 "Palette": {
95 "type": "object",
96 "properties": {
97 "palette_name": {
98 "type": "array",
99 "items": {
100 "$ref": "#/components/schemas/Color"
101 }
102 }
103 }
104 },
105 "Color": {
106 "type": "string"
107 }
108 }
109 },
110 "responses": {
111 "200": {
112 "description": "A list of colors (may be filtered by palette)",
113 "schema": {
114 "$ref": "#/components/schemas/Palette"
115 },
116 "examples": {
117 "rgb": [
118 "red",
119 "green",
120 "blue"
121 ]
122 }
123 }
124 }
125 }
126
127
128 swag = Swagger(app, config=swagger_config)
129
130
131 def test_swag(client, specs_data):
132 """
133 This test is runs automatically in Travis CI
134
135 :param client: Flask app test client
136 :param specs_data: {'url': {swag_specs}} for every spec in app
137 """
138 for spec in specs_data.values():
139 assert 'openapi' in spec
140 assert '3.0.2' == spec['openapi']
141 assert 'swagger' not in spec
142 assert 'components' in spec
143 assert 'securitySchemes' in spec['components']
144 assert 'oAuthSample' in spec['components']['securitySchemes']
145
146 assert 'servers' in spec # See issue #366
147
148
149 if __name__ == '__main__':
150 app.run(debug=True)