Skip to content

Commit a3f244d

Browse files
reupencarltongibson
authored andcommitted
Move AutoSchema serializer instantiation to common method
1 parent 7e1c4be commit a3f244d

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

rest_framework/schemas/openapi.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -419,23 +419,26 @@ def _map_field_validators(self, validators, schema):
419419
schema['maximum'] = int(digits * '9') + 1
420420
schema['minimum'] = -schema['maximum']
421421

422-
def _get_request_body(self, path, method):
422+
def _get_serializer(self, method, path):
423423
view = self.view
424424

425-
if method not in ('PUT', 'PATCH', 'POST'):
426-
return {}
427-
428425
if not hasattr(view, 'get_serializer'):
429-
return {}
426+
return None
430427

431428
try:
432-
serializer = view.get_serializer()
429+
return view.get_serializer()
433430
except exceptions.APIException:
434-
serializer = None
435431
warnings.warn('{}.get_serializer() raised an exception during '
436432
'schema generation. Serializer fields will not be '
437433
'generated for {} {}.'
438434
.format(view.__class__.__name__, method, path))
435+
return None
436+
437+
def _get_request_body(self, path, method):
438+
if method not in ('PUT', 'PATCH', 'POST'):
439+
return {}
440+
441+
serializer = self._get_serializer(path, method)
439442

440443
if not isinstance(serializer, serializers.Serializer):
441444
return {}
@@ -459,24 +462,15 @@ def _get_request_body(self, path, method):
459462
def _get_responses(self, path, method):
460463
# TODO: Handle multiple codes.
461464
content = {}
462-
view = self.view
463-
if hasattr(view, 'get_serializer'):
464-
try:
465-
serializer = view.get_serializer()
466-
except exceptions.APIException:
467-
serializer = None
468-
warnings.warn('{}.get_serializer() raised an exception during '
469-
'schema generation. Serializer fields will not be '
470-
'generated for {} {}.'
471-
.format(view.__class__.__name__, method, path))
472-
473-
if isinstance(serializer, serializers.Serializer):
474-
content = self._map_serializer(serializer)
475-
# No write_only fields for response.
476-
for name, schema in content['properties'].copy().items():
477-
if 'writeOnly' in schema:
478-
del content['properties'][name]
479-
content['required'] = [f for f in content['required'] if f != name]
465+
serializer = self._get_serializer(path, method)
466+
467+
if isinstance(serializer, serializers.Serializer):
468+
content = self._map_serializer(serializer)
469+
# No write_only fields for response.
470+
for name, schema in content['properties'].copy().items():
471+
if 'writeOnly' in schema:
472+
del content['properties'][name]
473+
content['required'] = [f for f in content['required'] if f != name]
480474

481475
return {
482476
'200': {

0 commit comments

Comments
 (0)