Правильный способ определения параметра списка в django-rest-framework и swagger
Фон
У меня есть следующее представление, и сопутствующий swagger UI генерируется django-spectacular:
class AllowedNameType(Enum):
BRAND = "BRAND"
....
@classmethod
def list(cls):
return list(map(lambda c: c.value, cls))
class GenerateCompanyNamesViewSet(viewsets.ViewSet):
http_method_names = ["get"]
def list(self, request: Request, *args, **kwargs) -> Response:
"""Generate suggested company names"""
# query_params: dict = {}
allowed_name_types: list[AllowedNameType] = query_params.get("allowed_name_types")
suggestions: list[Suggestion] = ...
return Response(serializers.SuggestionSerializer(suggestions).data)
Я хочу добиться следующего:
- The swagger UI should have a parameter
allowed_name_typeswhich is a list ofAllowedNameTypevalues - The input should be validated as per the serializer definition
- Type checking should be enforced on query_params to make sure the
allowed_name_typesis of typelist[AllowedNameType](ideally,allowed_name_typeswould actually be a named parameter in (eglist(..., allowed_name_types: list[AllowedNameType])
Попытка решения
class AllowedNameTypesParamSerializer(rest_framework_serializers.Serializer):
allowed_name_types = rest_framework_serializers.ListField(
child=rest_framework_serializers.ChoiceField(choices=models.AllowedNameType.list()),
required=False,
allow_empty=True,
)
и добавил следующий декоратор к методу list:
@extend_schema(
parameters=[
OpenApiParameter(name="allowed_name_types", required=True, type=AllowedNameTypesParamSerializer),
],
responses=serializers.FoodSearchAutoCompleteSerializer,
)
def list(....)
Это приводит к следующему интерфейсу: 
К сожалению:
- The swagger component expects a dictionary of
{"allowed_name_types:...}instead of a list - the
allowed_name_typesvalidation of list elements does not work (i.e I can put a value in the list that is not from AllowedNameType) - Strangely, calling
request.query_params.get('allowed_name_types')only returns the last value from the allowed_name_types.
Помощь?
Я уверен, что у меня есть все части лобзика для достижения того, что я хочу, но я не могу понять, как собрать их вместе, чтобы получить хорошо документированный API и типизированное проверенное поведение, которое мне нужно. Любая помощь будет очень признательна :)