Array of entities in request body
Related issue: https://github.com/ruby-grape/grape-entity/issues/252
As for example above, we want request body to be:
{
"accounts": [
{
"cma": 0,
"name": "string",
"environment": "string",
"sites": 0,
"username": "string",
"password": "string"
}
]
}
But actually:
[
{
"cma": 0,
"name": "string",
"environment": "string",
"sites": 0,
"username": "string",
"password": "string"
}
]
Grape also wants the array to be in the key of top level JSON object. ( Top level array data cannot be handled by Grape out of the box ( cf. https://github.com/ruby-grape/grape/issues/1730 ). )
So it seems the generated specification is broken.
A workaround is to adding dummy optional parameter. It forces the request body to be a JSON object.
I've found that making MoveParams#build_definition to always use object_type fixes the issue.
Is this reasonable fix?
I just ran into the same bug. Really don't want to go the dummy parameter route, but looks like it's not possible right now.
The issue seems to be with the should_expose_as_array? method in move_params.b
I have this param contract.
requires :members, type: Array, documentation: {param_type: 'body'} do
requires :user_id, type: Integer, desc: 'User ID'
requires :role, type: Symbol, values: [:viewer, :user, :manager, :remove], desc: 'Role for the user.'
end
And it yields incorrect swagger docs expecting this.
[
{
"members": [
{
"user_id": 0,
"role": "string"
}
]
}
]
When it should be like this.
{
"members": [
{
"user_id": 0,
"role": "string"
}
]
}
Hey, did you guys find out a workaround for the problem mentioned in this issue?
@swistaczek what I have been doing to work around this issue is the following.
params do
requires :members, type: Array, documentation: {param_type: 'body'} do
requires :user_id, type: Integer, desc: 'User ID'
requires :role, type: Symbol, values: [:viewer, :user, :manager, :remove], desc: 'Role for the user.'
end
optional :ignored, type: Boolean, desc: 'Workaround for https://github.com/ruby-grape/grape-swagger/issues/666'
end
not the most elegant solution at the moment. Note that I am in the middle of doing an upgrade to grape-swagger 1.0/grape 1.3.x and have not verified this yet against that version.
So.. I just tested on grape 1.3.x/ grape-swagger 1.1.0 and this issue appears to be resolved now.
@swistaczek I'm using a similar workaround:
params do
requires :contacts, type: Array[API::V2::Entities::Contact], documentation:
{ desc: 'Contacts', is_array: true, in: 'body' }
optional :hack, documentation: { in: 'body', desc: 'Hack to bypass Grape bug' }
end
@urkle I just tried (quickly) to upgrade to grape 1.3.3 and grape-swagger1.1.0 and still see the problem. 😢
With the "hack" the expected JSON is:
{
"contacts": [
{
"id": "d67197dd-3a5b-403a-8876-899f2e9468b6",
"name": "John"
}
],
"hack": "string"
}
Without the "hack" it (still) becomes:
[
{
"id": "5abd4d34-45c2-45ab-94a6-3bfbd0b335a9",
"name": "John"
}
]
So, this issue is fixed when the nested is an object, but when the nested is a scalar it still occurs. thus
params do
requires :members, type: Array, documentation: {param_type: 'body'} do
requires :user_id, type: Integer, desc: 'User ID'
requires :role, type: Symbol, values: [:viewer, :user, :manager, :remove], desc: 'Role for the user.'
end
end
works, but this does not work and instead creates a completely unusable expected input of [1,2,3] (with no key name).
params do
requires :rule_ids, type: Array[Integer], desc: 'blah', documentation: {param_type: 'body'}
end
Also, using in: 'body' does not work ANYWHERE For me, I have to use param_type: 'body'
Related bug #725
Hi there, I have read the solutions provided by different guys but my scenario is about filters. Which means there could be array of users like 1,4 or 10 or so on etc. How can i manage this?