ruby-grape / ruby-grape/grape

JSON support is still not first class

Open
#1,685 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug? discuss!
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

First class JSON support is confusing, as a subset of valid JSON is not supported due to the current implementation expecting the parsed result to either be a Hash or Array[Hash], while the output of JSON.parse could also be String, Integer, or mixed type arrays.

The initial thought would be to have JSON targeting all valid JSON, while being able to take modifiers to limit the possible valid outputs, like:

requires :foo, type: JSON # this could result in  `Hash`, `String`, `Integer`, or `Array`, accepting any of the inputs detailed in the two lines below
requires :bar, type: JSON[Hash] # this limits the output to `Hash`, so  "{\"a\":\"hash\"}" is valid while "1",  "\"foo\"", and "[1, 2]" would be invalid inputs
requires :baz, type: JSON[Hash, String] # this would accept "\"foo\"" and "{\"a\":\"hash\"}" as valid, while "1" and "[1, 2]" are invalid

In addition, when the API is given a type that is already what the JSON is parsed to (currently that would be Hash or Array[Hash]) it still attempts to coerce it despite being already valid. For example, "{\"a\":\"hash\"}" is currently valid while { a: 'hash' } is not despite the desired parsed representation being the same. A PR modifying that logic will be linked soon.

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 locating the Grape parameter type validation and JSON parsing logic described in the issue, then trace how JSON.parse results are checked and coerced. Compare the current Hash and Array[Hash] behavior with the proposed JSON and JSON[...] cases; done means valid JSON scalar and mixed-array inputs are handled as specified without unnecessary coercion.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.