mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
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:
parent
c69e2e4eaa
commit
c603b98403
|
@ -1063,7 +1063,8 @@ class OpenAPIRenderer(BaseRenderer):
|
|||
class JSONOpenAPIRenderer(BaseRenderer):
|
||||
media_type = 'application/vnd.oai.openapi+json'
|
||||
charset = None
|
||||
encoder_class = encoders.JSONEncoder
|
||||
format = 'openapi-json'
|
||||
|
||||
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')
|
||||
|
|
|
@ -11,7 +11,8 @@ from rest_framework.authtoken.views import obtain_auth_token
|
|||
from rest_framework.compat import uritemplate
|
||||
from rest_framework.parsers import JSONParser, MultiPartParser
|
||||
from rest_framework.renderers import (
|
||||
BaseRenderer, BrowsableAPIRenderer, JSONRenderer, OpenAPIRenderer
|
||||
BaseRenderer, BrowsableAPIRenderer, JSONOpenAPIRenderer, JSONRenderer,
|
||||
OpenAPIRenderer
|
||||
)
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator
|
||||
|
@ -992,6 +993,19 @@ class TestGenerator(TestCase):
|
|||
assert 'openapi' 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):
|
||||
patterns = []
|
||||
generator = SchemaGenerator(patterns=patterns)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import uuid
|
||||
from datetime import timedelta
|
||||
|
||||
from django.core.validators import (
|
||||
DecimalValidator, MaxLengthValidator, MaxValueValidator,
|
||||
|
@ -59,6 +60,7 @@ class DocStringExampleDetailView(APIView):
|
|||
class ExampleSerializer(serializers.Serializer):
|
||||
date = serializers.DateField()
|
||||
datetime = serializers.DateTimeField()
|
||||
duration = serializers.DurationField(default=timedelta())
|
||||
hstore = serializers.HStoreField()
|
||||
uuid_field = serializers.UUIDField(default=uuid.uuid4)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user