|
23 | 23 | # see http://www.gnu.org/licenses/.
|
24 | 24 | #
|
25 | 25 | # ##############################################################################
|
26 |
| -from rest_framework.schemas.openapi import SchemaGenerator |
| 26 | +from collections import OrderedDict |
| 27 | + |
| 28 | +from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator |
| 29 | +from rest_framework.serializers import Serializer |
| 30 | + |
| 31 | +from base.models.utils.utils import ChoiceEnum |
27 | 32 |
|
28 | 33 |
|
29 | 34 | class AdmissionSchemaGenerator(SchemaGenerator):
|
30 | 35 | def get_schema(self, *args, **kwargs):
|
31 | 36 | schema = super().get_schema(*args, **kwargs)
|
| 37 | + schema["openapi"] = "3.0.0" |
32 | 38 | schema["info"]["title"] = "Admission API"
|
33 | 39 | schema["info"]["description"] = "This API delivers data for the Admission project."
|
34 | 40 | schema["info"]["contact"] = {
|
@@ -63,4 +69,173 @@ def get_schema(self, *args, **kwargs):
|
63 | 69 | "description": "Enter your token in the format **Token <token>**"
|
64 | 70 | }
|
65 | 71 | }
|
| 72 | + schema['components']['parameters'] = { |
| 73 | + "X-User-FirstName": { |
| 74 | + "in": "header", |
| 75 | + "name": "X-User-FirstName", |
| 76 | + "schema": { |
| 77 | + "type": "string" |
| 78 | + }, |
| 79 | + "required": False |
| 80 | + }, |
| 81 | + "X-User-LastName": { |
| 82 | + "in": "header", |
| 83 | + "name": "X-User-LastName", |
| 84 | + "schema": { |
| 85 | + "type": "string" |
| 86 | + }, |
| 87 | + "required": False |
| 88 | + }, |
| 89 | + "X-User-Email": { |
| 90 | + "in": "header", |
| 91 | + "name": "X-User-Email", |
| 92 | + "schema": { |
| 93 | + "type": "string" |
| 94 | + }, |
| 95 | + "required": False |
| 96 | + }, |
| 97 | + "X-User-GlobalID": { |
| 98 | + "in": "header", |
| 99 | + "name": "X-User-GlobalID", |
| 100 | + "schema": { |
| 101 | + "type": "string" |
| 102 | + }, |
| 103 | + "required": False |
| 104 | + }, |
| 105 | + "Accept-Language": { |
| 106 | + "in": "header", |
| 107 | + "name": "Accept-Language", |
| 108 | + "description": "The header advertises which languages the client is able to understand, and which " |
| 109 | + "locale variant is preferred. (By languages, we mean natural languages, such as " |
| 110 | + "English, and not programming languages.)", |
| 111 | + "schema": { |
| 112 | + "$ref": "#/components/schemas/AcceptedLanguageEnum" |
| 113 | + }, |
| 114 | + "required": False |
| 115 | + } |
| 116 | + } |
| 117 | + schema['components']['responses'] = { |
| 118 | + "Unauthorized": { |
| 119 | + "description": "Unauthorized", |
| 120 | + "content": { |
| 121 | + "application/json": { |
| 122 | + "schema": { |
| 123 | + "$ref": "#/components/schemas/Error" |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }, |
| 128 | + "BadRequest": { |
| 129 | + "description": "Bad request", |
| 130 | + "content": { |
| 131 | + "application/json": { |
| 132 | + "schema": { |
| 133 | + "$ref": "#/components/schemas/Error" |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + }, |
| 138 | + "NotFound": { |
| 139 | + "description": "The specified resource was not found", |
| 140 | + "content": { |
| 141 | + "application/json": { |
| 142 | + "schema": { |
| 143 | + "$ref": "#/components/schemas/Error" |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + schema['components']['schemas']['Error'] = { |
| 150 | + "type": "object", |
| 151 | + "properties": { |
| 152 | + "code": { |
| 153 | + "type": "string" |
| 154 | + }, |
| 155 | + "message": { |
| 156 | + "type": "string" |
| 157 | + } |
| 158 | + }, |
| 159 | + "required": [ |
| 160 | + "code", |
| 161 | + "message" |
| 162 | + ] |
| 163 | + } |
| 164 | + schema['components']['schemas']['AcceptedLanguageEnum'] = { |
| 165 | + "type": "string", |
| 166 | + "enum": [ |
| 167 | + "en", |
| 168 | + "fr-be" |
| 169 | + ] |
| 170 | + } |
| 171 | + for path, path_content in schema['paths'].items(): |
| 172 | + for method, method_content in path_content.items(): |
| 173 | + method_content['responses'].update({ |
| 174 | + "400": { |
| 175 | + "$ref": "#/components/responses/BadRequest" |
| 176 | + }, |
| 177 | + "401": { |
| 178 | + "$ref": "#/components/responses/Unauthorized" |
| 179 | + }, |
| 180 | + "404": { |
| 181 | + "$ref": "#/components/responses/NotFound" |
| 182 | + } |
| 183 | + }) |
66 | 184 | return schema
|
| 185 | + |
| 186 | + |
| 187 | +class DetailedAutoSchema(AutoSchema): |
| 188 | + def __init__(self, *args, **kwargs): |
| 189 | + super().__init__(*args, **kwargs) |
| 190 | + self.enums = {} |
| 191 | + |
| 192 | + def get_request_body(self, path, method): |
| 193 | + if method not in ('PUT', 'PATCH', 'POST'): |
| 194 | + return {} |
| 195 | + |
| 196 | + self.request_media_types = self.map_parsers(path, method) |
| 197 | + |
| 198 | + serializer = self.get_serializer(path, method, for_response=False) |
| 199 | + |
| 200 | + if not isinstance(serializer, Serializer): |
| 201 | + item_schema = {} |
| 202 | + else: |
| 203 | + item_schema = self._get_reference(serializer) |
| 204 | + |
| 205 | + return { |
| 206 | + 'content': { |
| 207 | + ct: {'schema': item_schema} |
| 208 | + for ct in self.request_media_types |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + def get_components(self, path, method): |
| 213 | + if method.lower() == 'delete': |
| 214 | + return {} |
| 215 | + |
| 216 | + components = {} |
| 217 | + for with_response in [True, False]: |
| 218 | + serializer = self.get_serializer(path, method, for_response=with_response) |
| 219 | + if not isinstance(serializer, Serializer): |
| 220 | + return {} |
| 221 | + component_name = self.get_component_name(serializer) |
| 222 | + content = self.map_serializer(serializer) |
| 223 | + components[component_name] = content |
| 224 | + |
| 225 | + for enum_name, enum in self.enums.items(): |
| 226 | + components[enum_name] = enum |
| 227 | + |
| 228 | + return components |
| 229 | + |
| 230 | + def get_serializer(self, path, method, for_response=True): |
| 231 | + raise NotImplementedError |
| 232 | + |
| 233 | + def map_choicefield(self, field): |
| 234 | + # The only way to retrieve the original enum is to compare choices |
| 235 | + for declared_enum in ChoiceEnum.__subclasses__(): |
| 236 | + if OrderedDict(declared_enum.choices()) == field.choices: |
| 237 | + self.enums[declared_enum.__name__] = super().map_choicefield(field) |
| 238 | + return { |
| 239 | + '$ref': "#/components/responses/{}".format(declared_enum.__name__) |
| 240 | + } |
| 241 | + return super().map_choicefield(field) |
0 commit comments