Skip to content

Array of entities in request body #666

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

Open
yewton opened this issue Feb 14, 2018 · 9 comments
Open

Array of entities in request body #666

yewton opened this issue Feb 14, 2018 · 9 comments
Labels

Comments

@yewton
Copy link
Contributor

yewton commented Feb 14, 2018

Related issue: ruby-grape/grape-entity#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. ruby-grape/grape#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?

@lstanden
Copy link

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.

@urkle
Copy link
Contributor

urkle commented Feb 5, 2019

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"
      }
    ]
  }

@swistaczek
Copy link
Contributor

Hey, did you guys find out a workaround for the problem mentioned in this issue?

@urkle
Copy link
Contributor

urkle commented Jun 5, 2020

@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.

@urkle
Copy link
Contributor

urkle commented Jun 7, 2020

So.. I just tested on grape 1.3.x/ grape-swagger 1.1.0 and this issue appears to be resolved now.

@fotos
Copy link
Contributor

fotos commented Jun 8, 2020

@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"
  }
]

@urkle
Copy link
Contributor

urkle commented Feb 9, 2023

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'

@urkle
Copy link
Contributor

urkle commented Feb 9, 2023

Related bug #725

@Ehtisham-Ayaan
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants