Unknown tag on redered schema when using Django's SafeString as help_text #8402
Unanswered
hashlash
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The Issue
I have a model with SafeString as its help_text (inspired from Django's password validation help text):
I followed the DRF's doc about Generating a dynamic schema with SchemaView and A minimal example with Swagger UI, but the Swagger UI fails to render the generated schema.
The generated schema (full version):
Here's a repo for demonstrating the bug. The Swagger UI can be accessed on
/swagger-ui/
.Package versions:
Findings
I found that the schema generation for the help_text lies on:
django-rest-framework/rest_framework/schemas/openapi.py
Lines 289 to 290 in a53e523
and:
django-rest-framework/rest_framework/schemas/openapi.py
Lines 537 to 538 in a53e523
But since
SafeString
is a subclass ofstr
and it override the__str__()
method to return itself, those DRF's method will still return aSafeString
object.Workaround
I've with 3 possible workarounds, and so far I think the third is the best:
Adding the
SafeString
with an empty string (implementation, demo)Use
str.__str__()
(implementation, demo)Use
represent_str()
to representSafeString
asstr
(implementation, demo)PS: I also asked about the "string conversion" on Django's forum.
Beta Was this translation helpful? Give feedback.
All reactions