@@ -419,23 +419,26 @@ def _map_field_validators(self, validators, schema):
419
419
schema ['maximum' ] = int (digits * '9' ) + 1
420
420
schema ['minimum' ] = - schema ['maximum' ]
421
421
422
- def _get_request_body (self , path , method ):
422
+ def _get_serializer (self , method , path ):
423
423
view = self .view
424
424
425
- if method not in ('PUT' , 'PATCH' , 'POST' ):
426
- return {}
427
-
428
425
if not hasattr (view , 'get_serializer' ):
429
- return {}
426
+ return None
430
427
431
428
try :
432
- serializer = view .get_serializer ()
429
+ return view .get_serializer ()
433
430
except exceptions .APIException :
434
- serializer = None
435
431
warnings .warn ('{}.get_serializer() raised an exception during '
436
432
'schema generation. Serializer fields will not be '
437
433
'generated for {} {}.'
438
434
.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 )
439
442
440
443
if not isinstance (serializer , serializers .Serializer ):
441
444
return {}
@@ -459,24 +462,15 @@ def _get_request_body(self, path, method):
459
462
def _get_responses (self , path , method ):
460
463
# TODO: Handle multiple codes.
461
464
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 ]
480
474
481
475
return {
482
476
'200' : {
0 commit comments