Correct schema parsing for JSONField (#5878)

Fixes #5873.
* Use Object type. 
* Add test for field_to_schema
This commit is contained in:
Jimmy Merrild Krag 2018-04-20 15:47:20 +02:00 committed by Carlton Gibson
parent 9dbb49ef22
commit 5ee0e5df83
2 changed files with 44 additions and 0 deletions

View File

@ -95,6 +95,8 @@ def field_to_schema(field):
description=description, description=description,
format='date-time' format='date-time'
) )
elif isinstance(field, serializers.JSONField):
return coreschema.Object(title=title, description=description)
if field.style.get('base_template') == 'textarea.html': if field.style.get('base_template') == 'textarea.html':
return coreschema.String( return coreschema.String(
@ -102,6 +104,7 @@ def field_to_schema(field):
description=description, description=description,
format='textarea' format='textarea'
) )
return coreschema.String(title=title, description=description) return coreschema.String(title=title, description=description)

View File

@ -17,6 +17,7 @@ from rest_framework.schemas import (
AutoSchema, ManualSchema, SchemaGenerator, get_schema_view AutoSchema, ManualSchema, SchemaGenerator, get_schema_view
) )
from rest_framework.schemas.generators import EndpointEnumerator from rest_framework.schemas.generators import EndpointEnumerator
from rest_framework.schemas.inspectors import field_to_schema
from rest_framework.schemas.utils import is_list_view from rest_framework.schemas.utils import is_list_view
from rest_framework.test import APIClient, APIRequestFactory from rest_framework.test import APIClient, APIRequestFactory
from rest_framework.utils import formatting from rest_framework.utils import formatting
@ -763,6 +764,46 @@ class TestAutoSchema(TestCase):
link = view.schema.get_link(path, method, base_url) link = view.schema.get_link(path, method, base_url)
assert link == expected assert link == expected
def test_field_to_schema(self):
label = 'Test label'
help_text = 'This is a helpful test text'
cases = [
# tuples are ([field], [expected schema])
# TODO: Add remaining cases
(
serializers.BooleanField(label=label, help_text=help_text),
coreschema.Boolean(title=label, description=help_text)
),
(
serializers.DecimalField(1000, 1000, label=label, help_text=help_text),
coreschema.Number(title=label, description=help_text)
),
(
serializers.FloatField(label=label, help_text=help_text),
coreschema.Number(title=label, description=help_text)
),
(
serializers.IntegerField(label=label, help_text=help_text),
coreschema.Integer(title=label, description=help_text)
),
(
serializers.DateField(label=label, help_text=help_text),
coreschema.String(title=label, description=help_text, format='date')
),
(
serializers.DateTimeField(label=label, help_text=help_text),
coreschema.String(title=label, description=help_text, format='date-time')
),
(
serializers.JSONField(label=label, help_text=help_text),
coreschema.Object(title=label, description=help_text)
),
]
for case in cases:
self.assertEqual(field_to_schema(case[0]), case[1])
def test_docstring_is_not_stripped_by_get_description(): def test_docstring_is_not_stripped_by_get_description():
class ExampleDocstringAPIView(APIView): class ExampleDocstringAPIView(APIView):