From bc58f8fd9dbe4ba7b2efab5790838939db1e1ae2 Mon Sep 17 00:00:00 2001 From: "T. Franzel" Date: Wed, 1 Mar 2023 20:10:42 +0100 Subject: [PATCH] consolidate sort fix for enum sorting --- drf_spectacular/contrib/django_filters.py | 3 +-- drf_spectacular/plumbing.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/drf_spectacular/contrib/django_filters.py b/drf_spectacular/contrib/django_filters.py index 6d4578be..b848f924 100644 --- a/drf_spectacular/contrib/django_filters.py +++ b/drf_spectacular/contrib/django_filters.py @@ -152,8 +152,6 @@ def resolve_filter_field(self, auto_schema, model, filterset_class, field_name, # explicit filter choices may disable enum retrieved from model if not schema_from_override and filter_choices is not None: enum = filter_choices - if enum: - schema['enum'] = sorted(enum, key=str) description = schema.pop('description', None) if filter_field.extra.get('help_text', None): @@ -194,6 +192,7 @@ def resolve_filter_field(self, auto_schema, model, filterset_class, field_name, location=OpenApiParameter.QUERY, description=description, schema=schema, + enum=enum, explode=explode, style=style ) diff --git a/drf_spectacular/plumbing.py b/drf_spectacular/plumbing.py index 1a6cda9d..8db692c7 100644 --- a/drf_spectacular/plumbing.py +++ b/drf_spectacular/plumbing.py @@ -371,9 +371,9 @@ def build_parameter_type( if enum: # in case of array schema, enum makes little sense on the array itself if schema['schema'].get('type') == 'array': - schema['schema']['items']['enum'] = sorted(enum) + schema['schema']['items']['enum'] = sorted(enum, key=str) else: - schema['schema']['enum'] = sorted(enum) + schema['schema']['enum'] = sorted(enum, key=str) if pattern is not None: # in case of array schema, pattern only makes sense on the items if schema['schema'].get('type') == 'array':