ruby-grape / ruby-grape/grape-swagger

Singular params with Array[String] in body does not render correct JSON

Open
#725 14 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I hit a bug when trying to use this param declaration:

params do
  requires :tags, type: Array[String], documentation: { in: 'body' }
end

Expecting this to create a schema and tag it on the request, it actually did a formData instead:

{
  "in": "formData",
  "name": "tags",
  "type": "array",
  "items": {
    "type": "string"
  },
  "required": true
}

I've been using this successfully everywhere else so this threw me off. I experimented and realized that the requires with Array[String] does not work if it's the only one. It immediately works when you add a non array/string param:

params do
  requires :tags, type: Array[String], documentation: { in: 'body' }
  requires :lol, documentation: { in: 'body' }
end

Results in:

{
  "name": "V1PostMortemsReportsReportIdTags",
  "in": "body",
  "required": true,
  "schema": {
    "$ref": "#/definitions/putV1PostMortemsReportsReportIdTags"
  }
}

And the schema:

{
  "type": "object",
  "properties": {
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "lol": {
      "type": "string"
    }
  },
  "required": [
    "tags",
    "lol"
  ],
  "description": "Add tags to a report"
}

What's also peculiar is that if you add multiple Array[String] to params it maintains the same bug, I've only been able to get to work the moment you add a non-array param.

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 by reproducing the issue with a params block containing only an Array[String] declaration and documentation in the body, then compare the generated formData output with the expected body schema. Trace the parameter-to-request documentation entry point and add a regression case covering array-only parameters; done means the generated request uses in: body with a schema and preserves the array item type.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.