From 63f6377dff08305d79dcd274ba3c19ff48ea7e78 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Wed, 23 Aug 2017 11:13:30 +0200 Subject: [PATCH] Move `get_encoding` to descriptor. --- rest_framework/schemas.py | 50 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index f2e0a689b..809722ad2 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -282,10 +282,8 @@ class APIViewSchemaDescriptor(object): fields += self.get_pagination_fields(path, method) fields += self.get_filter_fields(path, method) - # TEMP: now we proxy back to the generator - if fields and any([field.location in ('form', 'body') for field in fields]): - encoding = generator.get_encoding(path, method, view) + encoding = self.get_encoding(path, method) else: encoding = None @@ -456,6 +454,29 @@ class APIViewSchemaDescriptor(object): fields += filter_backend().get_schema_fields(view) return fields + def get_encoding(self, path, method): + """ + Return the 'encoding' parameter to use for a given endpoint. + """ + view = self.view + + # Core API supports the following request encodings over HTTP... + supported_media_types = set(( + 'application/json', + 'application/x-www-form-urlencoded', + 'multipart/form-data', + )) + parser_classes = getattr(view, 'parser_classes', []) + for parser_class in parser_classes: + media_type = getattr(parser_class, 'media_type', None) + if media_type in supported_media_types: + return media_type + # Raw binary uploads are supported with "application/octet-stream" + if media_type == '*/*': + return 'application/octet-stream' + + return None + # TODO: Where should this live? # - We import APIView here. So we can't import the descriptor into `views` # - APIView is only used by SchemaView. @@ -640,29 +661,6 @@ class SchemaGenerator(object): field_name = 'id' return path.replace('{pk}', '{%s}' % field_name) - # Methods for generating each individual `Link` instance... - - def get_encoding(self, path, method, view): - """ - Return the 'encoding' parameter to use for a given endpoint. - """ - # Core API supports the following request encodings over HTTP... - supported_media_types = set(( - 'application/json', - 'application/x-www-form-urlencoded', - 'multipart/form-data', - )) - parser_classes = getattr(view, 'parser_classes', []) - for parser_class in parser_classes: - media_type = getattr(parser_class, 'media_type', None) - if media_type in supported_media_types: - return media_type - # Raw binary uploads are supported with "application/octet-stream" - if media_type == '*/*': - return 'application/octet-stream' - - return None - # Method for generating the link layout.... def get_keys(self, subpath, method, view):