JSON support is still not first class
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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