Represent SafeString as plain string on schema rendering. (#8429)

* Use SafeString.represent_str to represent SafeString as str

* Add SafeString yaml rendering test
This commit is contained in:
hashlash 2022-11-23 21:42:06 +07:00 committed by GitHub
parent c0d95cb967
commit ebde56b932
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View File

@ -19,6 +19,7 @@ from django.core.paginator import Page
from django.template import engines, loader from django.template import engines, loader
from django.urls import NoReverseMatch from django.urls import NoReverseMatch
from django.utils.html import mark_safe from django.utils.html import mark_safe
from django.utils.safestring import SafeString
from rest_framework import VERSION, exceptions, serializers, status from rest_framework import VERSION, exceptions, serializers, status
from rest_framework.compat import ( from rest_framework.compat import (
@ -1055,6 +1056,7 @@ class OpenAPIRenderer(BaseRenderer):
class Dumper(yaml.Dumper): class Dumper(yaml.Dumper):
def ignore_aliases(self, data): def ignore_aliases(self, data):
return True return True
Dumper.add_representer(SafeString, Dumper.represent_str)
return yaml.dump(data, default_flow_style=False, sort_keys=False, Dumper=Dumper).encode('utf-8') return yaml.dump(data, default_flow_style=False, sort_keys=False, Dumper=Dumper).encode('utf-8')

View File

@ -5,6 +5,7 @@ import pytest
from django.db import models from django.db import models
from django.test import RequestFactory, TestCase, override_settings from django.test import RequestFactory, TestCase, override_settings
from django.urls import path from django.urls import path
from django.utils.safestring import SafeString
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from rest_framework import filters, generics, pagination, routers, serializers from rest_framework import filters, generics, pagination, routers, serializers
@ -583,6 +584,11 @@ class TestOperationIntrospection(TestCase):
renderer.render(data) == b'o2:\n test: test\no1:\n test: test\n' # py <= 3.5 renderer.render(data) == b'o2:\n test: test\no1:\n test: test\n' # py <= 3.5
) )
def test_openapi_yaml_safestring_render(self):
renderer = OpenAPIRenderer()
data = {'o1': SafeString('test')}
assert renderer.render(data) == b'o1: test\n'
def test_serializer_filefield(self): def test_serializer_filefield(self):
path = '/{id}/' path = '/{id}/'
method = 'POST' method = 'POST'