-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Parameter of type "associative array" #380
Comments
Hi there, Any news on that? Associative arrays are all over the place in APIs (at least then ones I worked/work on) I suggest using the simple "keys" parameter: name: pop
description: population by country
type: array
keys:
type: string
description: country code (2 characters)
items:
type: integer
description: population (in thousands) Do you think it is feasible to add it? For now I am using "x-keys" with the same format, but it won't display in the generated documentation (unless I'm mistaken) |
Can you reference any public standard that defines such a format? Can you reference any public common API that exposes that format? Right now, the usage of this format seems esoteric. |
On the contrary, can you firmly state that absolutely no API would have any use for associative arrays? Just one example (because I do not know each and every API out there): https://apidocs.mailchimp.com/api/2.0/lists/webhooks.php Here the "actions" is a definite list that keeps growing. What if one day you could have any number of "actions" here, or even custom actions? The solution you propose would be to change the format of returned data so that it is not an associative array anymore, but an indexed array instead. Which would break existing code calling that API. Instead, having an associative array here (which is nothing more than a generic object), would allow for a seamless transition. Just my 2 cents. And authorizing a new property "keys" for "type: array" data does not seem that much esoteric to me. As long as something is constant (here it would be the items/values), it maskes sense to allow its declaration imho. Someone else asked for it here : #289 The given example is a list of data by country, with the country 2 letters code being the key (this is the example I quoted in my comment). |
I can't say that, but I can say that we don't aim to accommodate for any kind of API out there, so just because there is a way to do it, that's not reason enough for us to support it. As for the mailchimp API, that doesn't look anything like what the OP requested. It's just an array of objects which is already supported by the spec (though please correct me if I'm wrong). As for defining a generic object, you can do that today too. It's ill-advised, and we don't encourage it, but you can. That's pretty much saying you don't want to document your API (in most cases, not all). |
JSON Schema has something like this: name: pop
description: population by country
type: object
patternProperties:
'[A-Z]{2}':
type: integer
description: the population of the country indicated by the property name. This syntax is not imported by Swagger, though. I guess if this would be supported, that would be the way to go. |
That's indeed what I ended up using after some research. If Swagger does Thank you Paulo! Le lun. 14 sept. 2015 à 16:50, Paŭlo Ebermann notifications@github.com a
|
The JSON API Spec actually has this for it's "field" query param. |
Rack is a quite common piece of software used in frameworks like Rails and Sinatra that has long parsed query parameters like This has been like this for a very long time. See https://github.com/rack/rack/blob/1.6.4/lib/rack/utils.rb#L111-L168 for their code. I'd say there are tons of use cases out there like these where |
👍 |
1 similar comment
+1 |
Link to parent #565 |
+1 standard feature in all rails applications [1], also seems the only reasonable way to pass 1 or more! nested maps on through query strings. ie for essential for a GET request. Also see "What Does normalize_params Do?" section for consice description how parsing is done. [1] http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters |
Does anyone have a solution for this in the meantime? Obviously can't leave an endpoint undocumented. I'm assuming something like this work work in the meantime? {
"name": "filters[tms_id]",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
} |
I'm having a similar issue with array parameters. Does anyone have any suggestions how to describe an associative array in the query request such as: I've come close with the following:
But that comes out as: http://example.com/api/rest/content?type=search&id=test&options=20&options=10 |
Hello, stripe uses this for search by date API something like GET /charges?date[gte]=2016-01-01&date[lte]=2016-01-02 Is it related to your requirements? |
@nemenemsrouge yes!, that's the same request pattern in the Stripe date API. They are using Is there any example of how to define that style date API GET request via the Swagger / OpenAPI spec? I had a google but it appears the Stripe API docs are generated via a custom in-house tool ( https://www.quora.com/What-software-powers-the-Stripe-API-documentation ) |
@davidwhthomas I m sorry that I have no idea about how to do that... Do you think using a "query" parameter and a type=object can do the job? Maybe have to try... |
Technically, you can describe that today by naming the parameter |
... but that does not scale to M fields by N operators. date[gte], date[gt], date[lte], date[lt], date[ne], then same for fields a, b, c. ... x, y, z, (We don't use notation such as this in our APis; I'm just noting that it is a stretch to enumerate all combinations.) |
I didn't say it's a holistic solution, it can still be used to define explicit parameters if that's a requirement. I'm not familiar with Stripe's date API and it wasn't described as having varied inputs, only two. For two, it's definitely doable. |
Thanks a lot. I test that today ! |
Is there any reason something like this won't work?
|
See solution in #804 |
Let's use the "pet store" application as example
Imagine that you can send multiple "find" requests in one. For that, you pass an array of associative arrays, which contain a list of integers
Text version:
How would you define that ?
I suggest the following syntax addition : adding a new property "indexType" to give the type of the keys in an array (default: integer, 0-based)
Example definition using this syntax
The text was updated successfully, but these errors were encountered: