Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import base64
import contextlib
import datetime
from urllib import parse

from django import forms
Expand Down Expand Up @@ -1062,6 +1063,7 @@ class Dumper(yaml.Dumper):
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')


Expand Down
11 changes: 11 additions & 0 deletions rest_framework/utils/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ def default(self, obj):
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you using represent_scalar from existing code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is actually the function of yaml Dumper class for overriding the tag for different datatypes or python object.
so this function gives custom representation for timedelta and first argument is dumper obj where we are adding this function

12 changes: 12 additions & 0 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,18 @@ def test_schema_rendering_to_json(self):
assert b'"openapi": "' in ret
assert b'"default": "0.0"' in ret

def test_schema_rendering_to_yaml(self):
patterns = [
path('example/', views.ExampleGenericAPIView.as_view()),
]
generator = SchemaGenerator(patterns=patterns)

request = create_request('/')
schema = generator.get_schema(request=request)
ret = OpenAPIRenderer().render(schema)
assert b"openapi: " in ret
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain the asserts please? also it would be nice to see some additional tests if possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. this test renders openapi yaml definition from an example schema which has a duration field with a default value timedelta(0) and asserts is checking for "openapi" and the duration fields default value "0.0".

I think we can add checking the" !!python/object/apply:datetime.timedelta" tag is not in response.
please let me know if you have any suggestions for cases it would be a great.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can extra tests it would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added test case for rendering yaml with durationfield having minvalidator. Please check.

assert b"default: '0.0'" in ret

def test_schema_with_no_paths(self):
patterns = []
generator = SchemaGenerator(patterns=patterns)
Expand Down