mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-18 04:02:35 +03:00
fix OpenAPIRenderer for timedelta
This commit is contained in:
parent
376a5cbbba
commit
43f9bd2f45
|
@ -9,6 +9,7 @@ REST framework also provides an HTML renderer that renders the browsable API.
|
|||
|
||||
import base64
|
||||
import contextlib
|
||||
import datetime
|
||||
from urllib import parse
|
||||
|
||||
from django import forms
|
||||
|
@ -1056,6 +1057,7 @@ class OpenAPIRenderer(BaseRenderer):
|
|||
def ignore_aliases(self, data):
|
||||
return True
|
||||
Dumper.add_representer(SafeString, Dumper.represent_str)
|
||||
Dumper.add_representer(datetime.timedelta, encoders.CustomScalar.represent_timedelta)
|
||||
return yaml.dump(data, default_flow_style=False, sort_keys=False, Dumper=Dumper).encode('utf-8')
|
||||
|
||||
|
||||
|
|
|
@ -65,3 +65,14 @@ class JSONEncoder(json.JSONEncoder):
|
|||
elif hasattr(obj, '__iter__'):
|
||||
return tuple(item for item in obj)
|
||||
return super().default(obj)
|
||||
|
||||
|
||||
class CustomScalar:
|
||||
"""
|
||||
CustomScalar that knows how to encode timedelta that renderer
|
||||
can understand.
|
||||
"""
|
||||
@classmethod
|
||||
def represent_timedelta(cls, dumper, data):
|
||||
value = str(data.total_seconds())
|
||||
return dumper.represent_scalar('tag:yaml.org,2002:str', value)
|
||||
|
|
Loading…
Reference in New Issue
Block a user