Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (#1079) crash when generating schema for field with UUID choices. #1080

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from rest_framework.fields import empty
from rest_framework.settings import api_settings
from rest_framework.test import APIRequestFactory
from rest_framework.utils.encoders import JSONEncoder
from rest_framework.utils.mediatypes import _MediaType
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
from uritemplate import URITemplate
Expand Down Expand Up @@ -839,7 +840,7 @@ def load_enum_name_overrides():


def list_hash(lst):
return hashlib.sha256(json.dumps(list(lst), sort_keys=True).encode()).hexdigest()
return hashlib.sha256(json.dumps(list(lst), sort_keys=True, cls=JSONEncoder).encode()).hexdigest()


def anchor_pattern(pattern: str) -> str:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,29 @@ def get(self, request):
'items': {'$ref': '#/components/schemas/QualityLevelsEnum'},
'readOnly': True
}


def test_uuid_choices(no_warnings):

import uuid

class XSerializer(serializers.Serializer):
foo = serializers.ChoiceField(
choices=[
(uuid.UUID('93d7527f-de3c-4a76-9cc2-5578675630d4'), 'baz'),
(uuid.UUID('47a4b873-409e-4e43-81d5-fafc3faeb849'), 'bar')
]
)

class XAPIView(APIView):
@extend_schema(responses=XSerializer)
def get(self, request):
pass # pragma: no cover

schema = generate_schema('x', view=XAPIView)

assert 'FooEnum' in schema['components']['schemas']
assert schema['components']['schemas']['FooEnum']['enum'] == [
uuid.UUID('93d7527f-de3c-4a76-9cc2-5578675630d4'),
uuid.UUID('47a4b873-409e-4e43-81d5-fafc3faeb849')
]