Skip to content

Commit

Permalink
bugfix incorrect collision warning #233
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Dec 19, 2020
1 parent 59fa6f1 commit 469c484
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,9 @@ def _get_response_for_code(self, serializer, status_code, media_types=None):
name=paginated_name,
type=ResolvedComponent.SCHEMA,
schema=paginator.get_paginated_response_schema(schema),
object=paginated_name,
object=serializer,
)
self.registry.register(component)
self.registry.register_on_missing(component)
schema = component.ref
elif paginator:
schema = paginator.get_paginated_response_schema(schema)
Expand Down
4 changes: 2 additions & 2 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def __init__(self):
self._components = {}

def register(self, component: ResolvedComponent):
if component.key in self._components:
if component in self:
warn(
f'trying to re-register a {component.type} component with name '
f'{self._components[component.key].name}. this might lead to '
Expand All @@ -446,7 +446,7 @@ def register(self, component: ResolvedComponent):
self._components[component.key] = component

def register_on_missing(self, component: ResolvedComponent):
if component.key not in self._components:
if component not in self:
self._components[component.key] = component

def __contains__(self, component):
Expand Down
25 changes: 25 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,31 @@ class XViewset(viewsets.ReadOnlyModelViewSet):
assert substitution['properties']['results']['items']['$ref'] == '#/components/schemas/X'


def test_pagination_reusage(no_warnings):
class M7(models.Model):
pass

class XSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = M7

class XViewset(viewsets.ReadOnlyModelViewSet):
queryset = M7.objects.all()
serializer_class = XSerializer
pagination_class = pagination.LimitOffsetPagination

class YViewset(XViewset):
serializer_class = XSerializer

router = routers.SimpleRouter()
router.register('x', XViewset, basename='x')
router.register('y', YViewset, basename='y')
generator = SchemaGenerator(patterns=router.urls)
schema = generator.get_schema(request=None, public=True)
validate_schema(schema)


@mock.patch(
'drf_spectacular.settings.spectacular_settings.SECURITY',
[{'apiKeyAuth': []}]
Expand Down
7 changes: 5 additions & 2 deletions tests/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from tests import generate_schema


def test_serializer_name_reuse(warnings):
def test_serializer_name_reuse(capsys):
from rest_framework import routers

from drf_spectacular.generators import SchemaGenerator
Expand All @@ -29,7 +29,7 @@ class XSerializer(serializers.Serializer):

def x2():
class XSerializer(serializers.Serializer):
integer = serializers.IntegerField
integer = serializers.IntegerField()

return XSerializer

Expand All @@ -46,6 +46,9 @@ class X2Viewset(mixins.ListModelMixin, viewsets.GenericViewSet):
generator = SchemaGenerator(patterns=router.urls)
generator.get_schema(request=None, public=True)

stderr = capsys.readouterr().err
assert 'Encountered 2 components with identical names "X" and different classes' in stderr


def test_owned_serializer_naming_override_with_ref_name_collision(warnings):
class XSerializer(serializers.Serializer):
Expand Down

0 comments on commit 469c484

Please sign in to comment.