JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Issue with `fields` request params

Open
#282 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

As I'm playing with the Sparse Fieldsets, I encountered some issues.

With the following resources:

class UserResource < JSONAPI::Resource
  has_one :status, class_name: 'UserStatus'
  attributes :name, :email
end
class UserStatusResource < JSONAPI::Resource
  has_one :user
  attributes :foo, :bar
end

Relationships are removed from then response when using fields, so we can't link a user to his status.

/users?include=status&fields[users]=name

Association with custom class_name failed with fields.

/users?include=status&fields[users]=name&fields[user-statuses]=foo

returns:

{:errors=>
  [{:title=>"Invalid resource",
    :detail=>"user-statuses is not a valid resource.",
    :id=>nil,
    :href=>nil,
    :code=>101,
    :path=>nil,
    :links=>nil,
    :status=>"bad_request"},
   {:title=>"Invalid resource",
    :detail=>"user-statuses is not a valid resource.",
    :id=>nil,
    :href=>nil,
    :code=>101,
    :path=>nil,
    :links=>nil,
    :status=>"bad_request"}]}

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 with the UserResource and UserStatusResource examples and reproduce the two requests using include=status and the fields parameters. Trace sparse-fieldset handling for relationships and the custom class_name resource key. Done means the relationship remains linkable and user-statuses is accepted without the reported invalid-resource errors.

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.