mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 01:20:02 +03:00
Schemas: Map renderers/parsers for request/response media-types.
This commit is contained in:
parent
1cc4be47b4
commit
324242bf4e
|
@ -1,4 +1,5 @@
|
||||||
import warnings
|
import warnings
|
||||||
|
from operator import attrgetter
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
from django.core.validators import (
|
from django.core.validators import (
|
||||||
|
@ -8,7 +9,7 @@ from django.core.validators import (
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.encoding import force_str
|
from django.utils.encoding import force_str
|
||||||
|
|
||||||
from rest_framework import exceptions, serializers
|
from rest_framework import exceptions, renderers, serializers
|
||||||
from rest_framework.compat import uritemplate
|
from rest_framework.compat import uritemplate
|
||||||
from rest_framework.fields import _UnvalidatedField, empty
|
from rest_framework.fields import _UnvalidatedField, empty
|
||||||
|
|
||||||
|
@ -78,7 +79,9 @@ class SchemaGenerator(BaseSchemaGenerator):
|
||||||
|
|
||||||
class AutoSchema(ViewInspector):
|
class AutoSchema(ViewInspector):
|
||||||
|
|
||||||
content_types = ['application/json']
|
request_media_types = []
|
||||||
|
response_media_types = []
|
||||||
|
|
||||||
method_mapping = {
|
method_mapping = {
|
||||||
'get': 'Retrieve',
|
'get': 'Retrieve',
|
||||||
'post': 'Create',
|
'post': 'Create',
|
||||||
|
@ -336,6 +339,12 @@ class AutoSchema(ViewInspector):
|
||||||
self._map_min_max(field, content)
|
self._map_min_max(field, content)
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
if isinstance(field, serializers.FileField):
|
||||||
|
return {
|
||||||
|
'type': 'string',
|
||||||
|
'format': 'binary'
|
||||||
|
}
|
||||||
|
|
||||||
# Simplest cases, default to 'string' type:
|
# Simplest cases, default to 'string' type:
|
||||||
FIELD_CLASS_SCHEMA_TYPE = {
|
FIELD_CLASS_SCHEMA_TYPE = {
|
||||||
serializers.BooleanField: 'boolean',
|
serializers.BooleanField: 'boolean',
|
||||||
|
@ -430,9 +439,20 @@ class AutoSchema(ViewInspector):
|
||||||
pagination_class = getattr(self.view, 'pagination_class', None)
|
pagination_class = getattr(self.view, 'pagination_class', None)
|
||||||
if pagination_class:
|
if pagination_class:
|
||||||
return pagination_class()
|
return pagination_class()
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def map_parsers(self, path, method):
|
||||||
|
return list(map(attrgetter('media_type'), self.view.parser_classes))
|
||||||
|
|
||||||
|
def map_renderers(self, path, method):
|
||||||
|
media_types = []
|
||||||
|
for renderer in self.view.renderer_classes:
|
||||||
|
# BrowsableAPIRenderer not relevant to OpenAPI spec
|
||||||
|
if renderer == renderers.BrowsableAPIRenderer:
|
||||||
|
continue
|
||||||
|
media_types.append(renderer.media_type)
|
||||||
|
return media_types
|
||||||
|
|
||||||
def _get_serializer(self, method, path):
|
def _get_serializer(self, method, path):
|
||||||
view = self.view
|
view = self.view
|
||||||
|
|
||||||
|
@ -452,6 +472,8 @@ class AutoSchema(ViewInspector):
|
||||||
if method not in ('PUT', 'PATCH', 'POST'):
|
if method not in ('PUT', 'PATCH', 'POST'):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
self.request_media_types = self.map_parsers(path, method)
|
||||||
|
|
||||||
serializer = self._get_serializer(path, method)
|
serializer = self._get_serializer(path, method)
|
||||||
|
|
||||||
if not isinstance(serializer, serializers.Serializer):
|
if not isinstance(serializer, serializers.Serializer):
|
||||||
|
@ -469,7 +491,7 @@ class AutoSchema(ViewInspector):
|
||||||
return {
|
return {
|
||||||
'content': {
|
'content': {
|
||||||
ct: {'schema': content}
|
ct: {'schema': content}
|
||||||
for ct in self.content_types
|
for ct in self.request_media_types
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -482,6 +504,8 @@ class AutoSchema(ViewInspector):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.response_media_types = self.map_renderers(path, method)
|
||||||
|
|
||||||
item_schema = {}
|
item_schema = {}
|
||||||
serializer = self._get_serializer(path, method)
|
serializer = self._get_serializer(path, method)
|
||||||
|
|
||||||
|
@ -509,7 +533,7 @@ class AutoSchema(ViewInspector):
|
||||||
'200': {
|
'200': {
|
||||||
'content': {
|
'content': {
|
||||||
ct: {'schema': response_schema}
|
ct: {'schema': response_schema}
|
||||||
for ct in self.content_types
|
for ct in self.response_media_types
|
||||||
},
|
},
|
||||||
# description is a mandatory property,
|
# description is a mandatory property,
|
||||||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#responseObject
|
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#responseObject
|
||||||
|
|
|
@ -339,6 +339,30 @@ class TestOperationIntrospection(TestCase):
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def test_multipart_request_body_generation(self):
|
||||||
|
"""Test that a view's delete method generates a proper response body schema."""
|
||||||
|
path = '/{id}/'
|
||||||
|
method = 'POST'
|
||||||
|
|
||||||
|
class ItemSerializer(serializers.Serializer):
|
||||||
|
attachment = serializers.FileField()
|
||||||
|
|
||||||
|
class View(generics.CreateAPIView):
|
||||||
|
serializer_class = ItemSerializer
|
||||||
|
|
||||||
|
view = create_view(
|
||||||
|
View,
|
||||||
|
method,
|
||||||
|
create_request(path),
|
||||||
|
)
|
||||||
|
inspector = AutoSchema()
|
||||||
|
inspector.view = view
|
||||||
|
|
||||||
|
request_body = inspector._get_request_body(path, method)
|
||||||
|
assert 'multipart/form-data' in request_body['content']
|
||||||
|
attachment = request_body['content']['multipart/form-data']['schema']['properties']['attachment']
|
||||||
|
assert attachment['format'] == 'binary'
|
||||||
|
|
||||||
def test_retrieve_response_body_generation(self):
|
def test_retrieve_response_body_generation(self):
|
||||||
"""
|
"""
|
||||||
Test that a list of properties is returned for retrieve item views.
|
Test that a list of properties is returned for retrieve item views.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user