Add CustomFieldsDataFieldInspector for OpenAPI spec

This commit is contained in:
Jeremy Stretch 2020-08-26 14:36:45 -04:00
parent d2b7eb161c
commit bde25e69f8
2 changed files with 13 additions and 0 deletions

View File

@ -484,6 +484,7 @@ REST_FRAMEWORK = {
SWAGGER_SETTINGS = {
'DEFAULT_AUTO_SCHEMA_CLASS': 'utilities.custom_inspectors.NetBoxSwaggerAutoSchema',
'DEFAULT_FIELD_INSPECTORS': [
'utilities.custom_inspectors.CustomFieldsDataFieldInspector',
'utilities.custom_inspectors.JSONFieldInspector',
'utilities.custom_inspectors.NullableBooleanFieldInspector',
'utilities.custom_inspectors.SerializedPKRelatedFieldInspector',

View File

@ -5,6 +5,7 @@ from drf_yasg.utils import get_serializer_ref_name
from rest_framework.fields import ChoiceField
from rest_framework.relations import ManyRelatedField
from extras.api.customfields import CustomFieldsDataField
from utilities.api import ChoiceField, SerializedPKRelatedField, WritableNestedSerializer
@ -60,6 +61,17 @@ class NullableBooleanFieldInspector(FieldInspector):
return result
class CustomFieldsDataFieldInspector(FieldInspector):
def field_to_swagger_object(self, field, swagger_object_type, use_references, **kwargs):
SwaggerType, ChildSwaggerType = self._get_partial_types(field, swagger_object_type, use_references, **kwargs)
if isinstance(field, CustomFieldsDataField) and swagger_object_type == openapi.Schema:
return SwaggerType(type=openapi.TYPE_OBJECT)
return NotHandled
class JSONFieldInspector(FieldInspector):
"""Required because by default, Swagger sees a JSONField as a string and not dict
"""