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

Cannot create list parameters #703

Closed
dougalg opened this issue Apr 11, 2022 · 2 comments
Closed

Cannot create list parameters #703

dougalg opened this issue Apr 11, 2022 · 2 comments
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@dougalg
Copy link

dougalg commented Apr 11, 2022

Describe the bug
Using Python 3.6, Django 3.2.7, drf_spectacular latest, I am unable to create a list style parameter with an explicit OpenApiParameter annotation.

To Reproduce

from rest_framework.serializers import (ListField, CharField)
from drf_spectacular.utils import extend_schema, OpenApiParameter

@extend_schema(
		parameters=[
			OpenApiParameter(
				name='groupIds',
				location='query',
				type=ListField(child=CharField()),
				description='Group ids for which to fetch digests',
				required=True,
			)
		]
	)

I receive the following error:

<h1>AttributeError
       at /digests/api/schema</h1>
  <pre class="exception_value">'ListField' object has no attribute 'items'</pre>
  

Request Method: | GET
-- | --
http://localhost:8000/digests/api/schema
3.2.7
AttributeError
'ListField' object has no attribute 'items'
/usr/local/lib/python3.6/site-packages/drf_spectacular/plumbing.py, line 318, in build_parameter_type
/usr/local/bin/python
3.6.15

The internal error

/usr/local/lib/python3.6/site-packages/drf_spectacular/plumbing.py, line 318, in build_parameter_type

    ):

        irrelevant_field_meta = ['readOnly', 'writeOnly']

        if location == OpenApiParameter.PATH:

            irrelevant_field_meta += ['nullable', 'default']

        schema = {

            'in': location,

            'name': name,

            'schema': {k: v for k, v in schema.items() if k not in irrelevant_field_meta}, # <-------- here

     …

        }

        if description:

            schema['description'] = description

        if required or location == 'path':

            schema['required'] = True

        if deprecated:

Expected behavior
I should be able to define a list of strings as an API parameter.

@tfranzel
Copy link
Owner

Hi @dougalg,

this is invalid usage as the type hint indicated. no fields allowed here, but you could have also constructed this with a serializer that is equally not supported. this has only been raised once before, but I can imagine people ran into this more often.

currently you can only do this:

OpenApiParameter(name='test', type={'type': 'array', 'items': {'type': 'string'}})

improved warnings to guide the user. for convenience, next release will also support:

OpenApiParameter(name='test', type=str, many=True)

@tfranzel tfranzel added enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Apr 21, 2022
@dougalg
Copy link
Author

dougalg commented Apr 21, 2022

Thank you! I think that this would address my needs. I'll try it out, thanks a lot!

@dougalg dougalg closed this as completed Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants