|
3 | 3 | from collections.abc import Callable
|
4 | 4 | from typing import Any
|
5 | 5 |
|
6 |
| -from django.utils.translation import gettext_lazy as _ |
7 | 6 | from drf_spectacular.generators import SchemaGenerator
|
8 | 7 | from drf_spectacular.plumbing import (
|
9 | 8 | ResolvedComponent,
|
10 | 9 | build_array_type,
|
11 |
| - build_basic_type, |
12 | 10 | build_object_type,
|
13 | 11 | )
|
14 | 12 | from drf_spectacular.renderers import OpenApiJsonRenderer
|
15 | 13 | from drf_spectacular.settings import spectacular_settings
|
16 |
| -from drf_spectacular.types import OpenApiTypes |
17 |
| -from rest_framework.settings import api_settings |
18 | 14 |
|
19 | 15 | from authentik.api.apps import AuthentikAPIConfig
|
20 |
| -from authentik.api.pagination import PAGINATION_COMPONENT_NAME, PAGINATION_SCHEMA |
21 |
| - |
22 |
| -GENERIC_ERROR = build_object_type( |
23 |
| - description=_("Generic API Error"), |
24 |
| - properties={ |
25 |
| - "detail": build_basic_type(OpenApiTypes.STR), |
26 |
| - "code": build_basic_type(OpenApiTypes.STR), |
27 |
| - }, |
28 |
| - required=["detail"], |
29 |
| -) |
30 |
| -VALIDATION_ERROR = build_object_type( |
31 |
| - description=_("Validation Error"), |
32 |
| - properties={ |
33 |
| - api_settings.NON_FIELD_ERRORS_KEY: build_array_type(build_basic_type(OpenApiTypes.STR)), |
34 |
| - "code": build_basic_type(OpenApiTypes.STR), |
35 |
| - }, |
36 |
| - required=[], |
37 |
| - additionalProperties={}, |
38 |
| -) |
| 16 | +from authentik.api.v3.schema import GENERIC_ERROR, PAGINATION, VALIDATION_ERROR |
39 | 17 |
|
40 | 18 |
|
41 | 19 | def create_component(
|
@@ -71,35 +49,14 @@ def postprocess_schema_responses(
|
71 | 49 | <https://github.com/tfranzel/drf-spectacular/issues/101>.
|
72 | 50 | """
|
73 | 51 |
|
74 |
| - create_component(generator, PAGINATION_COMPONENT_NAME, PAGINATION_SCHEMA) |
75 |
| - |
76 |
| - generic_error = create_component(generator, "GenericError", GENERIC_ERROR) |
77 |
| - validation_error = create_component(generator, "ValidationError", VALIDATION_ERROR) |
| 52 | + generator.registry.register_on_missing(PAGINATION) |
| 53 | + generator.registry.register_on_missing(GENERIC_ERROR) |
| 54 | + generator.registry.register_on_missing(VALIDATION_ERROR) |
78 | 55 |
|
79 | 56 | for path in result["paths"].values():
|
80 | 57 | for method in path.values():
|
81 |
| - method["responses"].setdefault( |
82 |
| - "400", |
83 |
| - { |
84 |
| - "content": { |
85 |
| - "application/json": { |
86 |
| - "schema": validation_error.ref, |
87 |
| - } |
88 |
| - }, |
89 |
| - "description": "", |
90 |
| - }, |
91 |
| - ) |
92 |
| - method["responses"].setdefault( |
93 |
| - "403", |
94 |
| - { |
95 |
| - "content": { |
96 |
| - "application/json": { |
97 |
| - "schema": generic_error.ref, |
98 |
| - } |
99 |
| - }, |
100 |
| - "description": "", |
101 |
| - }, |
102 |
| - ) |
| 58 | + method["responses"].setdefault("400", VALIDATION_ERROR.ref) |
| 59 | + method["responses"].setdefault("403", GENERIC_ERROR.ref) |
103 | 60 |
|
104 | 61 | result["components"] = generator.registry.build(spectacular_settings.APPEND_COMPONENTS)
|
105 | 62 |
|
@@ -226,11 +183,9 @@ def postprocess_schema_simplify_paginated(
|
226 | 183 | ]
|
227 | 184 | content_response["schema"] = build_object_type(
|
228 | 185 | properties={
|
229 |
| - "pagination": { |
230 |
| - "$ref": f"#/components/schemas/{PAGINATION_COMPONENT_NAME}" |
231 |
| - }, |
| 186 | + "pagination": PAGINATION.ref, |
232 | 187 | "autocomplete": {"$ref": "#/components/schemas/Autocomplete"},
|
233 |
| - "results": build_array_type(schema={"$ref": actual_component.ref}), |
| 188 | + "results": build_array_type(schema=actual_component.ref), |
234 | 189 | },
|
235 | 190 | required=["pagination", "results", "autocomplete"],
|
236 | 191 | )
|
|
0 commit comments