Skip to content

Commit ea25219

Browse files
beruicPierre Chiquet
authored andcommitted
Correct schema parsing for JSONField (encode#5878)
Fixes encode#5873. * Use Object type. * Add test for field_to_schema
1 parent cc9bff5 commit ea25219

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

rest_framework/schemas/inspectors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ def field_to_schema(field):
9595
description=description,
9696
format='date-time'
9797
)
98+
elif isinstance(field, serializers.JSONField):
99+
return coreschema.Object(title=title, description=description)
98100

99101
if field.style.get('base_template') == 'textarea.html':
100102
return coreschema.String(
101103
title=title,
102104
description=description,
103105
format='textarea'
104106
)
107+
105108
return coreschema.String(title=title, description=description)
106109

107110

tests/test_schemas.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
AutoSchema, ManualSchema, SchemaGenerator, get_schema_view
1818
)
1919
from rest_framework.schemas.generators import EndpointEnumerator
20+
from rest_framework.schemas.inspectors import field_to_schema
2021
from rest_framework.schemas.utils import is_list_view
2122
from rest_framework.test import APIClient, APIRequestFactory
2223
from rest_framework.utils import formatting
@@ -763,6 +764,46 @@ class CustomView(APIView):
763764
link = view.schema.get_link(path, method, base_url)
764765
assert link == expected
765766

767+
def test_field_to_schema(self):
768+
label = 'Test label'
769+
help_text = 'This is a helpful test text'
770+
771+
cases = [
772+
# tuples are ([field], [expected schema])
773+
# TODO: Add remaining cases
774+
(
775+
serializers.BooleanField(label=label, help_text=help_text),
776+
coreschema.Boolean(title=label, description=help_text)
777+
),
778+
(
779+
serializers.DecimalField(1000, 1000, label=label, help_text=help_text),
780+
coreschema.Number(title=label, description=help_text)
781+
),
782+
(
783+
serializers.FloatField(label=label, help_text=help_text),
784+
coreschema.Number(title=label, description=help_text)
785+
),
786+
(
787+
serializers.IntegerField(label=label, help_text=help_text),
788+
coreschema.Integer(title=label, description=help_text)
789+
),
790+
(
791+
serializers.DateField(label=label, help_text=help_text),
792+
coreschema.String(title=label, description=help_text, format='date')
793+
),
794+
(
795+
serializers.DateTimeField(label=label, help_text=help_text),
796+
coreschema.String(title=label, description=help_text, format='date-time')
797+
),
798+
(
799+
serializers.JSONField(label=label, help_text=help_text),
800+
coreschema.Object(title=label, description=help_text)
801+
),
802+
]
803+
804+
for case in cases:
805+
self.assertEqual(field_to_schema(case[0]), case[1])
806+
766807

767808
def test_docstring_is_not_stripped_by_get_description():
768809
class ExampleDocstringAPIView(APIView):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy