You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
json_related_link helper doesn't seem to work? #16
Or... I don't understand how to use it. There are no specs for it, so it's hard to know which is the case. I'm trying to use it to assert that a certain relationship is present in the JSON. The problem is, the json_item helper doesn't pull the data['relationships'] over, and so the json_related_link helper fails b/c payload['relationships'] is nil.
I tried to fix the json_item helper to bring the relationships over, but then I'd need to update the associated payload registration b/c there was an unaccounted for key. At that point I determined that I didn't fully understand how things were intended to work. Thus, an Issue rather than a PR. ☹️
Sorry for the lack of docs - I think I needed it for something short-term but wasn't sure if it should be public API. This was actually a helper to grab http links, ie self/related links.
It sounds like we need a helper to grab the id/type of a given relationship. I'm thinking something like this, your thoughts?
I suppose outlining my usage would have helped. Sorry. We are serializing an Invitation object which represents an invitation to join an existing family (think of it like a team, org, etc...) in our app. A unique URL is generated and shared with someone you'd like to invite. The JSON looks like this:
I wanted to make sure the "sender" link was present, and the expected value. I thought I could use json_related_link(json_item, 'sender', api_linvitation_url(invitation)) to test the link at data['relationships']['sender']['links']['related'].
But as I said, json_item doesn't contain the relationships data. Also, the json_related_link helper expects there to be one more level in the data structure, a final 'href' key. But per the JSONAPI spec I'm pretty sure that's optional. the related key's value can be either a string, or an object with href.
Anyhow... that's my use case. Does that clarify things at all? Provide a way forward?
OK this helps! Yeah the purpose of json_related_link helper is exactly that, it's just the method hasn't been well supported.
But per the JSONAPI spec I'm pretty sure that's optional. the related key's value can be either a string, or an object with href.
I'm with you on the spec. But I think jsonapi-rb will always generate the href, no? Are you using an alternate serialization library?
Apologies on this method - I've been avoiding links on my projects (long story) so I used it briefly and haven't been supporting it well. I see some old code using it like
Activity
stevenharman commentedon Apr 24, 2018
The mentioned snippets:
jsonapi_spec_helpers/lib/jsonapi_spec_helpers/helpers.rb
Lines 11 to 15 in 2ef4f0a
jsonapi_spec_helpers/lib/jsonapi_spec_helpers/helpers.rb
Line 28 in 2ef4f0a
richmolj commentedon Apr 24, 2018
Sorry for the lack of docs - I think I needed it for something short-term but wasn't sure if it should be public API. This was actually a helper to grab http links, ie self/related links.
It sounds like we need a helper to grab the id/type of a given relationship. I'm thinking something like this, your thoughts?
This should be able to take the id/type of the
json_item, look it up in the payload and find the relevant relationships.It could use a separate helper to find the item (incl relationships) anywhere in the payload.
stevenharman commentedon Apr 24, 2018
I suppose outlining my usage would have helped. Sorry. We are serializing an
Invitationobject which represents an invitation to join an existing family (think of it like a team, org, etc...) in our app. A unique URL is generated and shared with someone you'd like to invite. The JSON looks like this:{ "data": { "id": "kJ2MCFW2JJmpVLn4xmM6dszx", "type": "invitations", "attributes": { "nickname": "frye", "accepted_at": "2018-04-24T03:00:20.748Z", "sent_at": "2018-04-24T03:00:19.474Z" }, "relationships": { "sender": { "links": { "related": "http://localhost:5000/api/accounts/4" }, "data": { "type": "accounts", "id": "4" } } }, "links": { "self": "http://localhost:5000/api/invitations/kJ2MCFW2JJmpVLn4xmM6dszx" } }, "meta": {}, "jsonapi": { "version": "1.0" } }I wanted to make sure the "sender" link was present, and the expected value. I thought I could use
json_related_link(json_item, 'sender', api_linvitation_url(invitation))to test the link atdata['relationships']['sender']['links']['related'].But as I said,
json_itemdoesn't contain therelationshipsdata. Also, thejson_related_linkhelper expects there to be one more level in the data structure, a final'href'key. But per the JSONAPI spec I'm pretty sure that's optional. therelatedkey's value can be either a string, or an object withhref.Anyhow... that's my use case. Does that clarify things at all? Provide a way forward?
richmolj commentedon Apr 24, 2018
OK this helps! Yeah the purpose of
json_related_linkhelper is exactly that, it's just the method hasn't been well supported.I'm with you on the spec. But I think jsonapi-rb will always generate the href, no? Are you using an alternate serialization library?
Apologies on this method - I've been avoiding links on my projects (long story) so I used it briefly and haven't been supporting it well. I see some old code using it like
It seems like a helper method might be missing though to clean that up even further
raw_json_itemmaybe?raw: true?