Skip to content

json_related_link helper doesn't seem to work? #16

Open

Description

@stevenharman

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. ☹️

Activity

  1. stevenharman commented on Apr 24, 2018

    @stevenharman
    ContributorAuthor

    The mentioned snippets:

    {}.tap do |item|
    item['id'] = data['id']
    item['jsonapi_type'] = data['type']
    item.merge!(data['attributes']) if data.has_key?('attributes')
    end

    link = payload['relationships'][assn_name]['links']['related']['href']

  2. richmolj commented on Apr 24, 2018

    @richmolj
    Contributor

    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?

    jsonapi_related(json_item) # => [{ id: 1, type: 'posts' }]
    jsonapi_related(json_item, true) # => [1,2,3] just ids

    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.

  3. stevenharman commented on Apr 24, 2018

    @stevenharman
    ContributorAuthor

    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:

    {
      "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 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?

  4. richmolj commented on Apr 24, 2018

    @richmolj
    Contributor

    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

    expect(json_related_link(json['data'][0], 'servers')).to eq('thelink')

    It seems like a helper method might be missing though to clean that up even further raw_json_item maybe? raw: true?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions