Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
clov3r opened this issue Oct 21, 2015 · 4 comments
Open

Comments

@clov3r
Copy link

clov3r commented Oct 21, 2015

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"
                }
            }
        }
    }
}
@lgebhardt
Copy link
Member

Type is a reserved attribute name in JSON API. See http://jsonapi.org/format/#document-resource-object-fields. You should be getting a warning for that attribute.

@clov3r
Copy link
Author

clov3r commented Oct 22, 2015

Ok, I've renamed the attribute :type to :activity_type, but that hasn't changed the issue. Will update the OP to reflect this change.

@clov3r
Copy link
Author

clov3r commented Oct 22, 2015

After updating to 0.6.1, (which fixed my previous issue, thank you JR authors), I now have added the NewRecordResource:

class Api::V1::NewRecordResource < JSONAPI::Resource
  has_one :user
  has_one :target, polymorphic: true
  attributes :previous_value, :new_value, :message, :created_at
end

And I get a new response, with the same empty "type" string:

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

Possibly helpful/related, http://localhost:3000/api/v1/new-records/1/relationships/target returns a similar lack of data:

{
    "links": {
        "self": "http://localhost:3000/api/v1/new-records/1/relationships/target",
        "related": "http://localhost:3000/api/v1/new-records/1/target"
    },
    "data": {
        "type": "",
        "id": "239"
    }
}

and http://localhost:3000/api/v1/new-records/1/target returns

{
    "data":null
}

@clov3r
Copy link
Author

clov3r commented Oct 22, 2015

The rails repository can be found here: https://github.com/clov3rly/recruiting-resources-api/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants