JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

JR not including type of polymorphic has_one when called to include it

Open
#509 4 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

The response from the server is missing the embedded target/data/type, or rather replacing it with the empty string...

class Api::V1::ActivityResource < JSONAPI::Resource
  has_one :user
  has_one :target, polymorphic: true
  attributes :previous_value, :new_value, :message, :created_at
  attribute :activity_type   # this is included for now as JSONAPI::Resources sends model type as "activities"
  def activity_type
    @model.type
  end
end
irb(main):001:0> Activity.first
  Activity Load (0.7ms)  SELECT  "activities".* FROM "activities"  ORDER BY "activities"."id" ASC LIMIT 1
=> #<NewRecord id: 1, 
          user_id: nil, 
        target_id: 239, 
      target_type: "Facility",   # <= target_type is correct
   previous_value: nil, 
        new_value: nil, 
          message: nil, 
             type: "NewRecord", 
       created_at: "2015-10-13 15:16:12", 
       updated_at: "2015-10-13 15:16:12">

http://localhost:3000/api/v1/activities/1?include=target,user:

{
    "data": {
        "id": "1",
        "type": "activities",
        "links": {
            "self": "http://localhost:3000/api/v1/activities/1"
        },
        "attributes": {
            "previous-value": null,
            "new-value": null,
            "message": null,
            "created-at": "2015-10-13T15:16:12.964Z",
            "type": "NewRecord"
        },
        "relationships": {
            "user": {
                "links": {
                    "self": "http://localhost:3000/api/v1/activities/1/relationships/user",
                    "related": "http://localhost:3000/api/v1/activities/1/user"
                },
                "data": null
            },
            "target": {
                "links": {
                    "self": "http://localhost:3000/api/v1/activities/1/relationships/target",
                    "related": "http://localhost:3000/api/v1/activities/1/target"
                },
                "data": {
                    "type": "",    // <== should be "Facility"
                    "id": "239"
                }
            }
        }
    }
}

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

Reproduce the shown ActivityResource request with include=target,user and inspect how the polymorphic has_one :target relationship builds its resource identifier. Verify the fix by confirming that the included relationship data reports type Facility and id 239 instead of an empty type.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.