| 1 |
from functools import wraps |
| 2 |
|
| 3 |
try: |
| 4 |
import coreschema |
| 5 |
except ImportError: |
| 6 |
coreschema = None |
| 7 |
|
| 8 |
from .. import openapi |
| 9 |
from ..utils import force_real_str |
| 10 |
from .base import FilterInspector, NotHandled, PaginatorInspector |
| 11 |
|
| 12 |
|
| 13 |
def ignore_assert_decorator(func): |
| 14 |
@wraps(func) |
| 15 |
def wrapper(*args, **kwargs): |
| 16 |
try: |
| 17 |
return func(*args, **kwargs) |
| 18 |
except AssertionError: |
| 19 |
return NotHandled |
| 20 |
|
| 21 |
return wrapper |
| 22 |
|
| 23 |
|
| 24 |
class DrfAPICompatInspector(PaginatorInspector, FilterInspector): |
| 25 |
def param_to_schema(self, param): |
| 26 |
return openapi.Parameter( |
| 27 |
name=param["name"], |
| 28 |
in_=param["in"], |
| 29 |
description=param.get("description"), |
| 30 |
required=param.get("required", False), |
| 31 |
**param["schema"], |
| 32 |
) |
| 33 |
|
| 34 |
def get_paginator_parameters(self, paginator): |
| 35 |
if hasattr(paginator, "get_schema_operation_parameters"): |
| 36 |
return list( |
| 37 |
map( |
| 38 |
self.param_to_schema, |
| 39 |
paginator.get_schema_operation_parameters(self.view), |
| 40 |
) |
| 41 |
) |
| 42 |
return NotHandled |
| 43 |
|
| 44 |
def get_filter_parameters(self, filter_backend): |
| 45 |
if hasattr(filter_backend, "get_schema_operation_parameters"): |
| 46 |
return list( |
| 47 |
map( |
| 48 |
self.param_to_schema, |
| 49 |
filter_backend.get_schema_operation_parameters(self.view), |
| 50 |
) |
| 51 |
) |
| 52 |
|
| 53 |
if hasattr(filter_backend, "get_filterset_class"): |
| 54 |
return self.get_filter_backend_params(filter_backend) |
| 55 |
|
| 56 |
return NotHandled |
| 57 |
|
| 58 |
def get_filter_backend_params(self, filter_backend): |
| 59 |
filterset_class = filter_backend.get_filterset_class( |
| 60 |
self.view, |
| 61 |
self.view.queryset, |
| 62 |
) |
| 63 |
|
| 64 |
if filterset_class is None: |
| 65 |
return NotHandled |
| 66 |
|
| 67 |
return [ |
| 68 |
openapi.Parameter( |
| 69 |
name=field_name, |
| 70 |
in_=openapi.IN_QUERY, |
| 71 |
required=field.extra.get("required", False), |
| 72 |
type=openapi.TYPE_STRING, |
| 73 |
description=str(field.label) if field.label else "", |
| 74 |
) |
| 75 |
for field_name, field in filterset_class.base_filters.items() |
| 76 |
] |
| 77 |
|
| 78 |
|
| 79 |
class CoreAPICompatInspector(PaginatorInspector, FilterInspector): |
| 80 |
"""Converts ``coreapi.Field``\\ s to :class:`.openapi.Parameter`\\ s for filters and |
| 81 |
paginators that implement a ``get_schema_fields`` method. |
| 82 |
""" |
| 83 |
|
| 84 |
@ignore_assert_decorator |
| 85 |
def get_paginator_parameters(self, paginator): |
| 86 |
fields = [] |
| 87 |
if hasattr(paginator, "get_schema_fields"): |
| 88 |
fields = paginator.get_schema_fields(self.view) |
| 89 |
|
| 90 |
return [self.coreapi_field_to_parameter(field) for field in fields] |
| 91 |
|
| 92 |
@ignore_assert_decorator |
| 93 |
def get_filter_parameters(self, filter_backend): |
| 94 |
fields = [] |
| 95 |
if hasattr(filter_backend, "get_schema_fields"): |
| 96 |
fields = filter_backend.get_schema_fields(self.view) |
| 97 |
return [self.coreapi_field_to_parameter(field) for field in fields] |
| 98 |
|
| 99 |
def coreapi_field_to_parameter(self, field): |
| 100 |
"""Convert an instance of `coreapi.Field` to a swagger :class:`.Parameter` |
| 101 |
object. |
| 102 |
|
| 103 |
:param coreapi.Field field: |
| 104 |
:rtype: openapi.Parameter |
| 105 |
""" |
| 106 |
location_to_in = { |
| 107 |
"query": openapi.IN_QUERY, |
| 108 |
"path": openapi.IN_PATH, |
| 109 |
"form": openapi.IN_FORM, |
| 110 |
"body": openapi.IN_FORM, |
| 111 |
} |
| 112 |
coreapi_types = { |
| 113 |
coreschema.Integer: openapi.TYPE_INTEGER, |
| 114 |
coreschema.Number: openapi.TYPE_NUMBER, |
| 115 |
coreschema.String: openapi.TYPE_STRING, |
| 116 |
coreschema.Boolean: openapi.TYPE_BOOLEAN, |
| 117 |
} |
| 118 |
|
| 119 |
coreschema_attrs = ["format", "pattern", "enum", "min_length", "max_length"] |
| 120 |
schema = field.schema |
| 121 |
return openapi.Parameter( |
| 122 |
name=field.name, |
| 123 |
in_=location_to_in[field.location], |
| 124 |
required=field.required, |
| 125 |
description=force_real_str(schema.description) if schema else None, |
| 126 |
type=coreapi_types.get(type(schema), openapi.TYPE_STRING), |
| 127 |
**{attr: getattr(schema, attr, None) for attr in coreschema_attrs}, |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
class DjangoRestResponsePagination(PaginatorInspector): |
| 132 |
"""Provides response schema pagination wrapping for django-rest-framework' |
| 133 |
LimitOffsetPagination, PageNumberPagination and CursorPagination |
| 134 |
""" |
| 135 |
|
| 136 |
def fix_paginated_property(self, key: str, value: dict): |
| 137 |
|
| 138 |
value.pop("example", None) |
| 139 |
if "nullable" in value: |
| 140 |
value["x-nullable"] = value.pop("nullable") |
| 141 |
if key in {"next", "previous"} and "format" not in value: |
| 142 |
value["format"] = "uri" |
| 143 |
return openapi.Schema(**value) |
| 144 |
|
| 145 |
def get_paginated_response(self, paginator, response_schema): |
| 146 |
if hasattr(paginator, "get_paginated_response_schema"): |
| 147 |
paginator_schema = paginator.get_paginated_response_schema(response_schema) |
| 148 |
if paginator_schema["type"] == openapi.TYPE_OBJECT: |
| 149 |
properties = { |
| 150 |
k: self.fix_paginated_property(k, v) |
| 151 |
for k, v in paginator_schema.pop("properties").items() |
| 152 |
} |
| 153 |
if "required" not in paginator_schema: |
| 154 |
paginator_schema.setdefault("required", []) |
| 155 |
for prop in ("count", "results"): |
| 156 |
if prop in properties: |
| 157 |
paginator_schema["required"].append(prop) |
| 158 |
return openapi.Schema(**paginator_schema, properties=properties) |
| 159 |
else: |
| 160 |
return openapi.Schema(**paginator_schema) |
| 161 |
|
| 162 |
return response_schema |