Skip to content

Commit

Permalink
inspectors: add support for JSONField (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
etene authored and axnsan12 committed Jul 19, 2019
1 parent 2e0f9a1 commit ab6444a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ to this list.
:class:`'drf_yasg.inspectors.ChoiceFieldInspector' <.inspectors.ChoiceFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.FileFieldInspector' <.inspectors.FileFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.DictFieldInspector' <.inspectors.DictFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.JSONFieldInspector' <.inspectors.JSONFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.HiddenFieldInspector' <.inspectors.HiddenFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.RecursiveFieldInspector' <.inspectors.RecursiveFieldInspector>`, |br| \
:class:`'drf_yasg.inspectors.SerializerMethodFieldInspector' <.inspectors.SerializerMethodFieldInspector>`, |br| \
Expand Down
1 change: 1 addition & 0 deletions src/drf_yasg/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'drf_yasg.inspectors.ChoiceFieldInspector',
'drf_yasg.inspectors.FileFieldInspector',
'drf_yasg.inspectors.DictFieldInspector',
'drf_yasg.inspectors.JSONFieldInspector',
'drf_yasg.inspectors.HiddenFieldInspector',
'drf_yasg.inspectors.RelatedFieldInspector',
'drf_yasg.inspectors.SerializerMethodFieldInspector',
Expand Down
6 changes: 3 additions & 3 deletions src/drf_yasg/inspectors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
)
from .field import (
CamelCaseJSONFilter, ChoiceFieldInspector, DictFieldInspector, FileFieldInspector, HiddenFieldInspector,
InlineSerializerInspector, RecursiveFieldInspector, ReferencingSerializerInspector, RelatedFieldInspector,
SerializerMethodFieldInspector, SimpleFieldInspector, StringDefaultFieldInspector
InlineSerializerInspector, JSONFieldInspector, RecursiveFieldInspector, ReferencingSerializerInspector,
RelatedFieldInspector, SerializerMethodFieldInspector, SimpleFieldInspector, StringDefaultFieldInspector
)
from .query import CoreAPICompatInspector, DjangoRestResponsePagination
from .view import SwaggerAutoSchema
Expand All @@ -24,7 +24,7 @@

# field inspectors
'InlineSerializerInspector', 'RecursiveFieldInspector', 'ReferencingSerializerInspector', 'RelatedFieldInspector',
'SimpleFieldInspector', 'FileFieldInspector', 'ChoiceFieldInspector', 'DictFieldInspector',
'SimpleFieldInspector', 'FileFieldInspector', 'ChoiceFieldInspector', 'DictFieldInspector', 'JSONFieldInspector',
'StringDefaultFieldInspector', 'CamelCaseJSONFilter', 'HiddenFieldInspector', 'SerializerMethodFieldInspector',

# view inspectors
Expand Down
14 changes: 13 additions & 1 deletion src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,23 @@ def field_to_swagger_object(self, field, swagger_object_type, use_references, **
return NotHandled


class JSONFieldInspector(FieldInspector):
"""Provides conversion for ``JSONField``."""

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, serializers.JSONField) and swagger_object_type == openapi.Schema:
return SwaggerType(type=openapi.TYPE_OBJECT)

return NotHandled


class StringDefaultFieldInspector(FieldInspector):
"""For otherwise unhandled fields, return them as plain :data:`.TYPE_STRING` objects."""

def field_to_swagger_object(self, field, swagger_object_type, use_references, **kwargs): # pragma: no cover
# TODO unhandled fields: TimeField JSONField
# TODO unhandled fields: TimeField
SwaggerType, ChildSwaggerType = self._get_partial_types(field, swagger_object_type, use_references, **kwargs)
return SwaggerType(type=openapi.TYPE_STRING)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,23 @@ class ArrayViewSet(viewsets.ModelViewSet):
swagger = generator.get_schema(None, True)
property_schema = swagger['definitions']['Array']['properties']['array']['items']
assert property_schema == openapi.Schema(title='Array', type=expected_type, enum=choices)


def test_json_field():
class TestJSONFieldSerializer(serializers.Serializer):
json = serializers.JSONField()

class JSONViewSet(viewsets.ModelViewSet):
serializer_class = TestJSONFieldSerializer

router = routers.DefaultRouter()
router.register(r'jsons', JSONViewSet, **_basename_or_base_name('jsons'))

generator = OpenAPISchemaGenerator(
info=openapi.Info(title='Test json field generator', default_version='v1'),
patterns=router.urls
)

swagger = generator.get_schema(None, True)
property_schema = swagger["definitions"]["TestJSONField"]["properties"]["json"]
assert property_schema == openapi.Schema(title='Json', type=openapi.TYPE_OBJECT)

0 comments on commit ab6444a

Please sign in to comment.