JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Filter values for related resources are wrapped in nested arrays?

Open
#1,110 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

I'm a little confused as to what's going on, but upgrading from 0.8.3 to 0.9 broke some filters I have in place. e.g.:

VERIFY_RADIUS_FILTER = ->(value, context) {
  return NoLimit.new unless context[:current_location].present?

  value
}

FILTER_RADIUS_FROM_LOCATION = ->(records, value, options) {
  location = options.dig(:context, :current_location)
  limit = Array(value).first

  return records unless location.present? && limit.present?

  records.near(location, Float(limit))
}

filter :radius, default: 100.0,
                verify: VERIFY_RADIUS_FILTER,
                apply: FILTER_RADIUS_FROM_LOCATION

I think this commit my be the culprit? https://github.com/cerebris/jsonapi-resources/commit/3a691b29adb3e7aff0743ef59e44556bedb260c9

From what I can tell, this results in Resource#verify_resource being called twice. For a custom filter, at least (which is what I've got), that means the raw value gets doubly-nested in an array: https://github.com/cerebris/jsonapi-resources/blob/19f4d7b59f92b05fa28c4f925391cf5998123308/lib/jsonapi/resource.rb#L753

Instead I wonder if that line should be something like:

filter_values += raw.is_a?(String) ? CSV.parse_line(raw) : Array(raw)

To make sure we don't wrap an array?

/cc @hidde-jan

Contributor guide

No contributing guide indexed for this repository

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 at lib/jsonapi/resource.rb around line 753 and compare the filter-value handling with commit 3a691b2. Reproduce the custom-filter case described in the issue, checking whether Resource#verify_resource causes nested arrays. Done means related-resource filter values are not unnecessarily wrapped while string parsing still works; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.