Fix OpenAPISchema rendering for timedelta (#7641)

* Add failing test when rendering to json a schema with timedelta

* Fix JSONOpenAPIRenderer for fields with default=timedelta()

* fix isort

* fix test for python 3.5

Co-authored-by: Pierre Chiquet <pierre.chiquet@ubikey.fr>
This commit is contained in:
Pierre Chiquet 2021-03-10 13:03:15 +01:00 committed by GitHub
parent c69e2e4eaa
commit c603b98403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -1063,7 +1063,8 @@ class OpenAPIRenderer(BaseRenderer):
class JSONOpenAPIRenderer(BaseRenderer): class JSONOpenAPIRenderer(BaseRenderer):
media_type = 'application/vnd.oai.openapi+json' media_type = 'application/vnd.oai.openapi+json'
charset = None charset = None
encoder_class = encoders.JSONEncoder
format = 'openapi-json' format = 'openapi-json'
def render(self, data, media_type=None, renderer_context=None): def render(self, data, media_type=None, renderer_context=None):
return json.dumps(data, indent=2).encode('utf-8') return json.dumps(data, cls=self.encoder_class, indent=2).encode('utf-8')

View File

@ -11,7 +11,8 @@ from rest_framework.authtoken.views import obtain_auth_token
from rest_framework.compat import uritemplate from rest_framework.compat import uritemplate
from rest_framework.parsers import JSONParser, MultiPartParser from rest_framework.parsers import JSONParser, MultiPartParser
from rest_framework.renderers import ( from rest_framework.renderers import (
BaseRenderer, BrowsableAPIRenderer, JSONRenderer, OpenAPIRenderer BaseRenderer, BrowsableAPIRenderer, JSONOpenAPIRenderer, JSONRenderer,
OpenAPIRenderer
) )
from rest_framework.request import Request from rest_framework.request import Request
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator
@ -992,6 +993,19 @@ class TestGenerator(TestCase):
assert 'openapi' in schema assert 'openapi' in schema
assert 'paths' in schema assert 'paths' in schema
def test_schema_rendering_to_json(self):
patterns = [
path('example/', views.ExampleGenericAPIView.as_view()),
]
generator = SchemaGenerator(patterns=patterns)
request = create_request('/')
schema = generator.get_schema(request=request)
ret = JSONOpenAPIRenderer().render(schema)
assert b'"openapi": "' in ret
assert b'"default": "0.0"' in ret
def test_schema_with_no_paths(self): def test_schema_with_no_paths(self):
patterns = [] patterns = []
generator = SchemaGenerator(patterns=patterns) generator = SchemaGenerator(patterns=patterns)

View File

@ -1,4 +1,5 @@
import uuid import uuid
from datetime import timedelta
from django.core.validators import ( from django.core.validators import (
DecimalValidator, MaxLengthValidator, MaxValueValidator, DecimalValidator, MaxLengthValidator, MaxValueValidator,
@ -59,6 +60,7 @@ class DocStringExampleDetailView(APIView):
class ExampleSerializer(serializers.Serializer): class ExampleSerializer(serializers.Serializer):
date = serializers.DateField() date = serializers.DateField()
datetime = serializers.DateTimeField() datetime = serializers.DateTimeField()
duration = serializers.DurationField(default=timedelta())
hstore = serializers.HStoreField() hstore = serializers.HStoreField()
uuid_field = serializers.UUIDField(default=uuid.uuid4) uuid_field = serializers.UUIDField(default=uuid.uuid4)