Skip to content

Commit

Permalink
add name override to @extend_schema_serializer #517
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Sep 16, 2021
1 parent f6e3f5b commit 93935c7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,8 @@ def _get_serializer_name(self, serializer, direction):
if serializer_extension and serializer_extension.get_name():
# library override mechanisms
name = serializer_extension.get_name()
elif has_override(serializer, 'component_name'):
name = get_override(serializer, 'component_name')
elif getattr(getattr(serializer, 'Meta', None), 'ref_name', None) is not None:
# local override mechanisms. for compatibility with drf-yasg we support meta ref_name,
# though we do not support the serializer inlining feature.
Expand Down
4 changes: 4 additions & 0 deletions drf_spectacular/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def extend_schema_serializer(
exclude_fields: Optional[List[str]] = None,
deprecate_fields: Optional[List[str]] = None,
examples: Optional[List[OpenApiExample]] = None,
component_name: Optional[str] = None,
) -> Callable[[F], F]:
"""
Decorator for the "serializer" kind. Intended for overriding default serializer behaviour that
Expand All @@ -413,6 +414,7 @@ def extend_schema_serializer(
schema. fields will still be exposed through the API.
:param deprecate_fields: fields to mark as deprecated while processing the serializer.
:param examples: define example data to serializer.
:param component_name: override default class name extraction.
"""
def decorator(klass):
if many is not None:
Expand All @@ -423,6 +425,8 @@ def decorator(klass):
set_override(klass, 'deprecate_fields', deprecate_fields)
if examples:
set_override(klass, 'examples', examples)
if component_name:
set_override(klass, 'component_name', component_name)
return klass

return decorator
Expand Down
5 changes: 4 additions & 1 deletion tests/test_extend_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from drf_spectacular.openapi import AutoSchema
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_field
from drf_spectacular.utils import (
OpenApiParameter, extend_schema, extend_schema_field, extend_schema_serializer,
)
from tests import assert_schema, generate_schema, get_response_schema


Expand All @@ -31,6 +33,7 @@ def to_representation(self, value):
return urlsafe_base64_encode(b'\xf0\xf1\xf2') # pragma: no cover


@extend_schema_serializer(component_name='GammaEpsilon')
class GammaSerializer(serializers.Serializer):
encoding = serializers.CharField()
image_data = CustomField()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_extend_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Gamma'
$ref: '#/components/schemas/GammaEpsilon'
description: ''
'500':
headers:
Expand Down Expand Up @@ -369,7 +369,7 @@ components:
- a
- b
type: string
Gamma:
GammaEpsilon:
type: object
properties:
encoding:
Expand Down

0 comments on commit 93935c7

Please sign in to comment.