Skip to content

Commit

Permalink
add SerializerMethodField self-reference hint test
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Oct 1, 2024
1 parent 9f0d166 commit 96a4f38
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3421,3 +3421,26 @@ class XViewSet(viewsets.ReadOnlyModelViewSet):
schema = generate_schema('/x', XViewSet)
assert schema['paths']['/x/']['get']["operationId"] == 'list_x'
assert schema['paths']['/x/{id}/']['get']["operationId"] == 'retrieve_x'


class SelfReferentialSerializer(serializers.Serializer):
field_int = serializers.IntegerField()
field_self = serializers.SerializerMethodField()

def get_field_self(self) -> "SelfReferentialSerializer":
return SelfReferentialSerializer() # pragma: no cover


def test_self_referential_serializer_method_field(no_warnings):
class XViewset(viewsets.ModelViewSet):
serializer_class = SelfReferentialSerializer
queryset = SimpleModel.objects.all()

schema = generate_schema('/x', XViewset)
assert schema['components']['schemas']['SelfReferential']['properties'] == {
'field_int': {'type': 'integer'},
'field_self': {
'allOf': [{'$ref': '#/components/schemas/SelfReferential'}],
'readOnly': True
}
}

0 comments on commit 96a4f38

Please sign in to comment.