mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 13:54:47 +03:00
Add type annotation to schema generation
This commit is contained in:
parent
1084dca22b
commit
7edee804aa
|
@ -276,6 +276,10 @@ class HTMLFormRenderer(BaseRenderer):
|
|||
'base_template': 'input.html',
|
||||
'input_type': 'number'
|
||||
},
|
||||
serializers.FloatField: {
|
||||
'base_template': 'input.html',
|
||||
'input_type': 'number'
|
||||
},
|
||||
serializers.DateTimeField: {
|
||||
'base_template': 'input.html',
|
||||
'input_type': 'datetime-local'
|
||||
|
|
|
@ -15,10 +15,25 @@ from rest_framework.request import clone_request
|
|||
from rest_framework.response import Response
|
||||
from rest_framework.settings import api_settings
|
||||
from rest_framework.utils import formatting
|
||||
from rest_framework.utils.field_mapping import ClassLookupDict
|
||||
from rest_framework.views import APIView
|
||||
|
||||
|
||||
header_regex = re.compile('^[a-zA-Z][0-9A-Za-z_]*:')
|
||||
|
||||
types_lookup = ClassLookupDict({
|
||||
serializers.Field: 'string',
|
||||
serializers.IntegerField: 'integer',
|
||||
serializers.FloatField: 'number',
|
||||
serializers.DecimalField: 'number',
|
||||
serializers.BooleanField: 'boolean',
|
||||
serializers.FileField: 'file',
|
||||
serializers.MultipleChoiceField: 'array',
|
||||
serializers.ManyRelatedField: 'array',
|
||||
serializers.Serializer: 'object',
|
||||
serializers.ListSerializer: 'array'
|
||||
})
|
||||
|
||||
|
||||
def as_query_fields(items):
|
||||
"""
|
||||
|
@ -372,7 +387,14 @@ class SchemaGenerator(object):
|
|||
serializer = view.get_serializer()
|
||||
|
||||
if isinstance(serializer, serializers.ListSerializer):
|
||||
return [coreapi.Field(name='data', location='body', required=True)]
|
||||
return [
|
||||
coreapi.Field(
|
||||
name='data',
|
||||
location='body',
|
||||
required=True,
|
||||
type='array'
|
||||
)
|
||||
]
|
||||
|
||||
if not isinstance(serializer, serializers.Serializer):
|
||||
return []
|
||||
|
@ -388,7 +410,8 @@ class SchemaGenerator(object):
|
|||
name=field.source,
|
||||
location='form',
|
||||
required=required,
|
||||
description=description
|
||||
description=description,
|
||||
type=types_lookup[field]
|
||||
)
|
||||
fields.append(field)
|
||||
|
||||
|
|
|
@ -137,8 +137,8 @@ class TestRouterGeneratedSchema(TestCase):
|
|||
action='post',
|
||||
encoding='application/json',
|
||||
fields=[
|
||||
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||
coreapi.Field('b', required=False, location='form')
|
||||
coreapi.Field('a', required=True, location='form', type='string', description='A field description'),
|
||||
coreapi.Field('b', required=False, location='form', type='string')
|
||||
]
|
||||
),
|
||||
'retrieve': coreapi.Link(
|
||||
|
@ -155,8 +155,8 @@ class TestRouterGeneratedSchema(TestCase):
|
|||
description='A description of custom action.',
|
||||
fields=[
|
||||
coreapi.Field('pk', required=True, location='path'),
|
||||
coreapi.Field('c', required=True, location='form'),
|
||||
coreapi.Field('d', required=False, location='form'),
|
||||
coreapi.Field('c', required=True, location='form', type='string'),
|
||||
coreapi.Field('d', required=False, location='form', type='string'),
|
||||
]
|
||||
),
|
||||
'custom_list_action': coreapi.Link(
|
||||
|
@ -179,8 +179,8 @@ class TestRouterGeneratedSchema(TestCase):
|
|||
encoding='application/json',
|
||||
fields=[
|
||||
coreapi.Field('pk', required=True, location='path'),
|
||||
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||
coreapi.Field('b', required=False, location='form')
|
||||
coreapi.Field('a', required=True, location='form', type='string', description='A field description'),
|
||||
coreapi.Field('b', required=False, location='form', type='string')
|
||||
]
|
||||
),
|
||||
'partial_update': coreapi.Link(
|
||||
|
@ -189,8 +189,8 @@ class TestRouterGeneratedSchema(TestCase):
|
|||
encoding='application/json',
|
||||
fields=[
|
||||
coreapi.Field('pk', required=True, location='path'),
|
||||
coreapi.Field('a', required=False, location='form', description='A field description'),
|
||||
coreapi.Field('b', required=False, location='form')
|
||||
coreapi.Field('a', required=False, location='form', type='string', description='A field description'),
|
||||
coreapi.Field('b', required=False, location='form', type='string')
|
||||
]
|
||||
),
|
||||
'destroy': coreapi.Link(
|
||||
|
|
Loading…
Reference in New Issue
Block a user