| 1 |
|
| 2 |
import inspect |
| 3 |
|
| 4 |
from flask.views import MethodView |
| 5 |
|
| 6 |
import flasgger |
| 7 |
|
| 8 |
try: |
| 9 |
import marshmallow |
| 10 |
from marshmallow import fields |
| 11 |
from apispec.ext.marshmallow import openapi |
| 12 |
from apispec import APISpec as BaseAPISpec |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
openapi_converter = openapi.OpenAPIConverter( |
| 19 |
openapi_version='2.0', |
| 20 |
schema_name_resolver=lambda schema: None, |
| 21 |
spec=None |
| 22 |
) |
| 23 |
schema2jsonschema = openapi_converter.schema2jsonschema |
| 24 |
schema2parameters = openapi_converter.schema2parameters |
| 25 |
|
| 26 |
class Schema(marshmallow.Schema): |
| 27 |
swag_in = "body" |
| 28 |
swag_validate = True |
| 29 |
swag_validation_function = None |
| 30 |
swag_validation_error_handler = None |
| 31 |
swag_require_data = True |
| 32 |
|
| 33 |
def to_specs_dict(self): |
| 34 |
specs = {'parameters': self.__class__} |
| 35 |
definitions = {} |
| 36 |
specs.update(convert_schemas(specs, definitions)) |
| 37 |
specs['definitions'] = definitions |
| 38 |
return specs |
| 39 |
|
| 40 |
except ImportError: |
| 41 |
Schema = None |
| 42 |
fields = None |
| 43 |
schema2jsonschema = lambda schema: {} |
| 44 |
schema2parameters = lambda schema, location: [] |
| 45 |
BaseAPISpec = object |
| 46 |
|
| 47 |
|
| 48 |
class APISpec(BaseAPISpec): |
| 49 |
""" |
| 50 |
Wrapper around APISpec to add `to_flasgger` method |
| 51 |
""" |
| 52 |
|
| 53 |
def to_flasgger(self, app=None, definitions=None, paths=None): |
| 54 |
""" |
| 55 |
Converts APISpec dict to flasgger suitable dict |
| 56 |
also adds definitions and paths (optional) |
| 57 |
""" |
| 58 |
if Schema is None: |
| 59 |
raise RuntimeError('Please install marshmallow and apispec') |
| 60 |
|
| 61 |
return flasgger.utils.apispec_to_template( |
| 62 |
app, |
| 63 |
self, |
| 64 |
definitions=definitions, |
| 65 |
paths=paths |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
class SwaggerView(MethodView): |
| 70 |
""" |
| 71 |
A Swagger view |
| 72 |
""" |
| 73 |
parameters = [] |
| 74 |
responses = {} |
| 75 |
definitions = {} |
| 76 |
tags = [] |
| 77 |
consumes = ['application/json'] |
| 78 |
produces = ['application/json'] |
| 79 |
schemes = [] |
| 80 |
security = [] |
| 81 |
deprecated = False |
| 82 |
operationId = None |
| 83 |
externalDocs = {} |
| 84 |
summary = None |
| 85 |
description = None |
| 86 |
validation = False |
| 87 |
validation_function = None |
| 88 |
validation_error_handler = None |
| 89 |
|
| 90 |
def dispatch_request(self, *args, **kwargs): |
| 91 |
""" |
| 92 |
If validation=True perform validation |
| 93 |
""" |
| 94 |
if self.validation: |
| 95 |
specs = {} |
| 96 |
attrs = flasgger.constants.OPTIONAL_FIELDS + [ |
| 97 |
'parameters', 'definitions', 'responses', |
| 98 |
'summary', 'description' |
| 99 |
] |
| 100 |
for attr in attrs: |
| 101 |
specs[attr] = getattr(self, attr) |
| 102 |
definitions = {} |
| 103 |
specs.update(convert_schemas(specs, definitions)) |
| 104 |
specs['definitions'] = definitions |
| 105 |
flasgger.utils.validate( |
| 106 |
specs=specs, validation_function=self.validation_function, |
| 107 |
validation_error_handler=self.validation_error_handler |
| 108 |
) |
| 109 |
return super(SwaggerView, self).dispatch_request(*args, **kwargs) |
| 110 |
|
| 111 |
|
| 112 |
def convert_schemas(d, definitions=None): |
| 113 |
""" |
| 114 |
Convert Marshmallow schemas to dict definitions |
| 115 |
|
| 116 |
Also updates the optional definitions argument with any definitions |
| 117 |
entries contained within the schema. |
| 118 |
""" |
| 119 |
if definitions is None: |
| 120 |
definitions = {} |
| 121 |
definitions.update(d.get('definitions', {})) |
| 122 |
|
| 123 |
new = {} |
| 124 |
for k, v in d.items(): |
| 125 |
if isinstance(v, dict): |
| 126 |
v = convert_schemas(v, definitions) |
| 127 |
if isinstance(v, (list, tuple)): |
| 128 |
new_v = [] |
| 129 |
for item in v: |
| 130 |
if isinstance(item, dict): |
| 131 |
new_v.append(convert_schemas(item, definitions)) |
| 132 |
else: |
| 133 |
new_v.append(item) |
| 134 |
v = new_v |
| 135 |
if inspect.isclass(v) and issubclass(v, Schema): |
| 136 |
|
| 137 |
if Schema is None: |
| 138 |
raise RuntimeError('Please install marshmallow and apispec') |
| 139 |
|
| 140 |
definitions[v.__name__] = schema2jsonschema(v) |
| 141 |
ref = { |
| 142 |
"$ref": "#/definitions/{0}".format(v.__name__) |
| 143 |
} |
| 144 |
if k == 'parameters': |
| 145 |
new[k] = schema2parameters(v, location=v.swag_in) |
| 146 |
new[k][0]['schema'] = ref |
| 147 |
if len(definitions[v.__name__]['required']) != 0: |
| 148 |
new[k][0]['required'] = True |
| 149 |
else: |
| 150 |
new[k] = ref |
| 151 |
else: |
| 152 |
new[k] = v |
| 153 |
|
| 154 |
|
| 155 |
if 'definitions' in new: |
| 156 |
del new['definitions'] |
| 157 |
|
| 158 |
return new |