Skip to content

Commit

Permalink
Disable yaml aliases for schema generation. (encode#7131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten authored and sigvef committed Dec 3, 2022
1 parent 4ce458e commit 17f4e22
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,11 @@ def __init__(self):
assert yaml, 'Using OpenAPIRenderer, but `pyyaml` is not installed.'

def render(self, data, media_type=None, renderer_context=None):
return yaml.dump(data, default_flow_style=False, sort_keys=False).encode('utf-8')
# disable yaml advanced feature 'alias' for clean, portable, and readable output
class Dumper(yaml.Dumper):
def ignore_aliases(self, data):
return True
return yaml.dump(data, default_flow_style=False, sort_keys=False, Dumper=Dumper).encode('utf-8')


class JSONOpenAPIRenderer(BaseRenderer):
Expand Down
15 changes: 14 additions & 1 deletion tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework import filters, generics, pagination, routers, serializers
from rest_framework.compat import uritemplate
from rest_framework.parsers import JSONParser, MultiPartParser
from rest_framework.renderers import JSONRenderer
from rest_framework.renderers import JSONRenderer, OpenAPIRenderer
from rest_framework.request import Request
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator

Expand Down Expand Up @@ -473,6 +473,19 @@ class View(generics.CreateAPIView):
assert len(success_response['content'].keys()) == 1
assert 'application/json' in success_response['content']

def test_openapi_yaml_rendering_without_aliases(self):
renderer = OpenAPIRenderer()

reused_object = {'test': 'test'}
data = {
'o1': reused_object,
'o2': reused_object,
}
assert (
renderer.render(data) == b'o1:\n test: test\no2:\n test: test\n' or
renderer.render(data) == b'o2:\n test: test\no1:\n test: test\n' # py <= 3.5
)

def test_serializer_filefield(self):
path = '/{id}/'
method = 'POST'
Expand Down

0 comments on commit 17f4e22

Please sign in to comment.