Add form field descriptions to schemas (#4387)

This commit is contained in:
Tom Christie 2016-08-11 16:18:33 +01:00 committed by GitHub
parent 01b498ec51
commit 116917dbed
2 changed files with 12 additions and 5 deletions

View File

@ -4,6 +4,7 @@ from django.conf import settings
from django.contrib.admindocs.views import simplify_regex
from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
from django.utils import six
from django.utils.encoding import force_text
from rest_framework import exceptions, serializers
from rest_framework.compat import coreapi, uritemplate, urlparse
@ -295,7 +296,13 @@ class SchemaGenerator(object):
if field.read_only:
continue
required = field.required and method != 'PATCH'
field = coreapi.Field(name=field.source, location='form', required=required)
description = force_text(field.help_text) if field.help_text else ''
field = coreapi.Field(
name=field.source,
location='form',
required=required,
description=description
)
fields.append(field)
return fields

View File

@ -24,7 +24,7 @@ class ExamplePagination(pagination.PageNumberPagination):
class ExampleSerializer(serializers.Serializer):
a = serializers.CharField(required=True)
a = serializers.CharField(required=True, help_text='A field description')
b = serializers.CharField(required=False)
@ -131,7 +131,7 @@ class TestRouterGeneratedSchema(TestCase):
action='post',
encoding='application/json',
fields=[
coreapi.Field('a', required=True, location='form'),
coreapi.Field('a', required=True, location='form', description='A field description'),
coreapi.Field('b', required=False, location='form')
]
),
@ -162,7 +162,7 @@ class TestRouterGeneratedSchema(TestCase):
encoding='application/json',
fields=[
coreapi.Field('pk', required=True, location='path'),
coreapi.Field('a', required=True, location='form'),
coreapi.Field('a', required=True, location='form', description='A field description'),
coreapi.Field('b', required=False, location='form')
]
),
@ -172,7 +172,7 @@ class TestRouterGeneratedSchema(TestCase):
encoding='application/json',
fields=[
coreapi.Field('pk', required=True, location='path'),
coreapi.Field('a', required=False, location='form'),
coreapi.Field('a', required=False, location='form', description='A field description'),
coreapi.Field('b', required=False, location='form')
]
),