@@ -2574,6 +2574,46 @@ def create(self, request):
25742574 )
25752575
25762576
2577+ @pytest .mark .parametrize ('disable' , [False , True ])
2578+ def test_disable_docstring_descriptions (no_warnings , disable ):
2579+ class XSerializer (serializers .Serializer ):
2580+ """serializer docstring"""
2581+ field = serializers .CharField ()
2582+ field_method = serializers .SerializerMethodField ()
2583+
2584+ def get_field_method (self ) -> str :
2585+ """method field docstring"""
2586+ return '' # pragma: no cover
2587+
2588+ class XViewset (viewsets .ModelViewSet ):
2589+ """view class docstring"""
2590+ serializer_class = XSerializer
2591+ queryset = SimpleModel .objects .none ()
2592+
2593+ def retrieve (self , request ):
2594+ """action docstring"""
2595+ pass # pragma: no cover
2596+
2597+ with mock .patch (
2598+ 'drf_spectacular.settings.spectacular_settings.DISABLE_DOCSTRING_DESCRIPTIONS' , disable
2599+ ):
2600+ schema = generate_schema ('/x' , XViewset )
2601+
2602+ if disable :
2603+ assert 'description' not in schema ['paths' ]['/x/' ]['get' ]
2604+ assert 'description' not in schema ['paths' ]['/x/{id}/' ]['get' ]
2605+ assert 'description' not in schema ['components' ]['schemas' ]['X' ]
2606+ assert 'description' not in schema ['components' ]['schemas' ]['X' ]['properties' ]['field_method' ]
2607+ else :
2608+ assert schema ['paths' ]['/x/' ]['get' ]['description' ] == 'view class docstring'
2609+ assert schema ['paths' ]['/x/{id}/' ]['get' ]['description' ] == 'action docstring'
2610+ assert schema ['components' ]['schemas' ]['X' ]['description' ] == 'serializer docstring'
2611+ assert (
2612+ schema ['components' ]['schemas' ]['X' ]['properties' ]['field_method' ]['description' ]
2613+ == 'method field docstring'
2614+ )
2615+
2616+
25772617@pytest .mark .parametrize ('list_variation' , [
25782618 serializers .ListField , serializers .ListSerializer
25792619])
0 commit comments