ruby-grape / ruby-grape/grape-swagger

Array of entities in request body

Open
#666 9 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discuss!
Dominant language
Ruby
Stars
1.1k
Forks
479
Avg merge
2d 14h
Merged PRs (30d)
6

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/grape-swagger/doc_methods/move_params.rb at MoveParams#build_definition, then read the related issue and the request-body examples. Compare the generated specification with Grape's requirement for an object containing the array under a top-level key. Done means the specification represents the intended JSON object without requiring a dummy optional parameter.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.