1

The following swagger definition:

swagger: '2.0' host: example.com paths: /theResource: get: parameters: - $ref: '#/parameters/fields' parameters: fields: name: fields description: Fields to return in: query type: array collectionFormat: multi uniqueItems: true items: type: string enum: - column1 - column2 - column3 

Will generate the following URL, and the server will only receive fields equal to column3.

http://example.com/theResource?fields=column1&fields=column2&fields=column3 

How do I change it so that it sends an array in the parameter? For instance:

http://example.com/theResource?fields[]=column1&fields[]=column2&fields[]=column3 
5
  • Maybe just change the name from fields to fields[]? Commented Sep 30, 2016 at 16:05
  • By the way, the term you're looking for is YAML (not swagger - swagger is the product) Commented Sep 30, 2016 at 16:06
  • @ʰᵈˑ Yes, it is YAML, but it could also be JSON. Maybe I should have said "Open API specification"? Commented Sep 30, 2016 at 16:15
  • No. It's YAML (definately not JSON). Commented Sep 30, 2016 at 16:24
  • @ʰᵈˑ I showed definitely YAML format, but could have expressed the Open API spec (i.e Swagger spec) in JSON just as easily. Commented Oct 1, 2016 at 2:44

1 Answer 1

1

In PHP you can use http_build_query, that will construct proper query part of URL for you.

<?php $query = http_build_query([ 'fields' => [ 'one', 'two', 'three' ], ]); $url = "https://example.com/?".$query; 

And yes, the fields[] syntax should do the trick.

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

1 Comment

Thanks Andrzej, however, I know how to do so using PHP, but wish to do so using the Swagger/OpenAPI spec.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.