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

List Type in OpenApiTypes #341

Closed
CITIZENDOT opened this issue Mar 19, 2021 · 7 comments
Closed

List Type in OpenApiTypes #341

CITIZENDOT opened this issue Mar 19, 2021 · 7 comments

Comments

@CITIZENDOT
Copy link

CITIZENDOT commented Mar 19, 2021

Only dict is supported currently as OpenApiTypes.OBJECT, How to specify list type?
I guess it had to be added here.

@tfranzel
Copy link
Owner

you have to use a ListSerializer(), ListField(), or generally many=True. OpenApiTypes.LIST would make little sense as there would be no type for the list items. OpenApiTypes only contains basic types and that is by design.

maybe you actually want to use @extend_schema(responses=serializers.ListField(child=serializers.JSONField())

if you really want to do this manually: build_array_type(build_basic_type(OpenApiTypes.STR)) but that should be rarely necessary.

@CITIZENDOT
Copy link
Author

Thankyou. How can I use that in type argument of OpenApiParameter? ListField and ListField didn't seem to work there.

@tfranzel
Copy link
Owner

tfranzel commented Mar 19, 2021

my bad... serializers.ListSerializer(child=serializers.IntegerField()) will probably work better. the documentation should list accepted types i believe

@tfranzel
Copy link
Owner

closing this as it is not a bug or viable feature.

@elonzh
Copy link

elonzh commented Jul 7, 2021

I am using 0.17.2 and trying to add a list type for parameters, but it is not working as you described.

When using ListSerializer(child=CharField()):

@extend_schema(
    parameters=[
        OpenApiParameter(
            "tags__name",
            type=ListField(child=CharField()),
            style="form",
            explode=True,
            location=OpenApiParameter.QUERY,
            required=False,
        ),
    ]
)

Report:

...
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/views.py", line 67, in get
    return self._get_schema_response(request)
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/views.py", line 74, in _get_schema_response
    return Response(generator.get_schema(request=request, public=self.serve_public))
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/generators.py", line 257, in get_schema
    paths=self.parse(request, public),
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/generators.py", line 231, in parse
    operation = view.schema.get_operation(
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/utils.py", line 273, in get_operation
    return super().get_operation(path, path_regex, path_prefix, method, registry)
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 64, in get_operation
    parameters = self._get_parameters()
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 195, in _get_parameters
    override_parameters = self._process_override_parameters()
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 144, in _process_override_parameters
    schema = self.resolve_serializer(parameter.type, 'request').ref
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 1196, in resolve_serializer
    name=self._get_serializer_name(serializer, direction),
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 1171, in _get_serializer_name
    return self._get_serializer_name(serializer.child, direction)
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 1178, in _get_serializer_name
    if is_patched_serializer(serializer, direction):
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/plumbing.py", line 97, in is_patched_serializer
    and serializer.partial
AttributeError: 'CharField' object has no attribute 'partial'

When using ListField(child=CharField()):

@extend_schema(
    parameters=[
        OpenApiParameter(
            "tags__name",
            type=ListField(child=CharField()),
            style="form",
            explode=True,
            location=OpenApiParameter.QUERY,
            required=False,
        ),
    ]
)

Report:

...
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/views.py", line 67, in get
    return self._get_schema_response(request)
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/views.py", line 74, in _get_schema_response
    return Response(generator.get_schema(request=request, public=self.serve_public))
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/generators.py", line 257, in get_schema
    paths=self.parse(request, public),
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/generators.py", line 231, in parse
    operation = view.schema.get_operation(
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/utils.py", line 273, in get_operation
    return super().get_operation(path, path_regex, path_prefix, method, registry)
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 64, in get_operation
    parameters = self._get_parameters()
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 195, in _get_parameters
    override_parameters = self._process_override_parameters()
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 151, in _process_override_parameters
    result[parameter.name, parameter.location] = build_parameter_type(
  File "/home/elonzh/miniconda/envs/unob/lib/python3.8/site-packages/drf_spectacular/plumbing.py", line 255, in build_parameter_type
    'schema': {k: v for k, v in schema.items() if k not in irrelevant_field_meta},
AttributeError: 'ListField' object has no attribute 'items'

@berzi
Copy link

berzi commented Mar 19, 2024

This doesn't work for me either.

# in extend_schema() decorator:
OpenApiParameter(
  "myparam",
  Serializer(
      CharField(read_only=True, allow_null=False),
      many=True,
  ),
  location="query",
),

causes:

Warning [MyViewSet]: unsupported type for parameter "myparam". Skipping.

list[str] is not an accepted value, ListSerializer given as below causes the same warning.

ListSerializer(
  child=CharField(read_only=True, allow_null=False),
),

@3ng7n33r
Copy link

serializers.ListSerializer(child=serializers.IntegerField() Does not work for me neither. I get the same error message as berzi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants