0

I am using django-rest-swagger==2.1.2 and djangorestframework==3.7.7 and I have a view that looks like this:

class PendingRequests(generics.GenericAPIView):
    ...
    filter_backends = (PendingRequestsFilter,)

    def get(self, request):
        ...
        request_status = request.GET.getlist('status')
        request_start_date = request.GET.get('start_date')
        request_end_date = request.GET.get('end_date')
        ...

and I have defined a filter like so:

class PendingRequestsFilter(BaseFilterBackend):
    def get_schema_fields(self, view):
        fields = [
            coreapi.Field(name="status", description="Statuses of the Requests", required=False, location='query', example='active'),
            coreapi.Field(name="status", description="Statuses of the Requests", required=False, location='query', example='pending'),
            coreapi.Field(name="status", description="Statuses of the Requests", required=False, location='query', example='inactive'),
            coreapi.Field(name="start_date", description="Start Date of the Request (YYYY-MM-DD)", required=False, location='query', example='2525-12-25'),
            coreapi.Field(name="end_date", description="End Date of the Request (YYYY-MM-DD)", required=False, location='query', example='2525-12-26'),
        ]
        return fields

This results in my swagger UI for this view looking like this:

enter image description here

But when I enter different status like so ...

enter image description here

... and click 'try it out' I get a Request URL that looks like this:

http://myserver:4800/api/v1/sudorequests/requests/?status=pending&status=pending&status=pending

Note that status=pending is repeated three times. I wanted to get a Request URL that looks more like this:

http://myserver:4800/api/v1/sudorequests/requests/?status=inactive&status=active&status=pending
3
  • Which version of Django are you using? Commented Oct 2, 2018 at 15:59
  • Nevermind I managed to reproduce your problem Commented Oct 2, 2018 at 16:25
  • Thanks! I suspect it is a bug with Swagger UI. Commented Oct 2, 2018 at 16:44

1 Answer 1

2
+50

I have some insights that may help you.

Swagger Spec mentions allowMultiple setting in paragraph 5.2.4 Parameter Object

allowMultiple Another way to allow multiple values for a “query” parameter. If used, the query parameter may accept comma-separated values. The field may be used only if paramType is "query", "header" or "path".

That might what you want

I tried passing it to coreapi.Field as such:

coreapi.Field(name="status", description="Statuses of the Requests", required=False, location='query', example='active', allowMultiple=True)

but it doesn't work

Next I searched through swagger-ui.js and discovered this line

var isArray = paramType.toLowerCase() === 'array' || param.allowMultiple;

Luckily for us coreapi.Field is just a namedtuple, which passes all necessary params to swagger up. It looks like this:

Field = namedtuple('Field', ['name', 'required', 'location', 'schema', 'description', 'type', 'example'])
Field.__new__.__defaults__ = (False, '', None, None, None, None)

My idea is to extend it and use it instead of coreapi.Field. I'm monkeypatching because some coreapi.Document and coreapi.Link logic forces us to.

coreapi.Field = namedtuple('Field', coreapi.Field._fields + ('allowMultiple',))
coreapi.Field.__new__.__defaults__ = (False, '', None, None, None, None, False)

and then use it like this:

coreapi.Field(name="status", description="Statuses of the Requests", required=False, location='query', example='active', allowMultiple=True)

Unfortuantely still there is an error, but I decided to post it anyway. Maybe together we can figure it out. I'm still working on it and feel that i'm almost there.

EDIT

I've tried to overcome the error but it slowly becomes apparent that if you really need that feature it would be easier to just fork coreapi library and edit Field as i showed above.

Sign up to request clarification or add additional context in comments.

9 Comments

What file did you put your patch into?
just above class PendingRequestsFilter(BaseFilterBackend):
I've forked the coreapi repo and modified Field. Still no success. Looks like allowMultiple is not passed as spec to swagger-ui
I have opened an issue against swagger UI on github: github.com/swagger-api/swagger-ui/issues/4918
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.