diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 93fc7a739..bc5b4b603 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -377,7 +377,7 @@ class AutoSchema(ViewInspector): if field.default and field.default != empty: # why don't they use None?! schema['default'] = field.default if field.help_text: - schema['description'] = field.help_text + schema['description'] = force_text(field.help_text) self._map_field_validators(field.validators, schema) properties[field.field_name] = schema diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index ccffa750d..5b0195714 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -1,6 +1,7 @@ import pytest from django.conf.urls import url from django.test import RequestFactory, TestCase, override_settings +from django.utils.translation import ugettext_lazy as _ from rest_framework import filters, generics, pagination, routers, serializers from rest_framework.compat import uritemplate @@ -273,6 +274,15 @@ class TestOperationIntrospection(TestCase): assert response_schema['ip']['type'] == 'string' assert 'format' not in response_schema['ip'] + def test_lazy_string_field(self): + class Serializer(serializers.Serializer): + text = serializers.CharField(help_text=_('lazy string')) + + inspector = AutoSchema() + + data = inspector._map_serializer(Serializer()) + assert isinstance(data['properties']['text']['description'], str), "description must be str" + @pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.') @override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.openapi.AutoSchema'})