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

Serialising relationship arrays is not handled #80

Closed
duk3luk3 opened this issue Dec 29, 2017 · 3 comments
Closed

Serialising relationship arrays is not handled #80

duk3luk3 opened this issue Dec 29, 2017 · 3 comments
Labels
Milestone

Comments

@duk3luk3
Copy link
Contributor

duk3luk3 commented Dec 29, 2017

Trying to post this with kitsu:

    container_users = container_users.map(x => ({
        id: x,
        type: 'users'
    }));

    api.create('container', {
        name: container_name,
        'creator': {
            'id': container_creator,
            'type': 'users'
        },
        'host': {
            'id': container_host,
            'type': 'hosts'
        },
        'users': container_users
    }).then(res => console.log(res)
    ).catch((reason) => console.log(reason));

results in this being POSTed:

{
  "data": {
    "type": "containers",
    "attributes": {
      "name": "testcontainer3",
      "users": [
        {
          "id": 1,
          "type": "users"
        },
        {
          "id": 2,
          "type": "users"
        }
      ]
    },
    "relationships": {
      "creator": {
        "data": {
          "id": 1,
          "type": "users"
        }
      },
      "host": {
        "data": {
          "id": 1,
          "type": "hosts"
        }
      }
    }
  },
  "headers": {
    "Authorization": "foobar",
    "accept": "application\/vnd.api+json",
    "content-type": "application\/vnd.api+json"
  }
}

it puts users into attributes instead of relationships, where it should go.

This is because the relationship test in serialise.js does not test for arrays.

@wopian wopian added this to the 4.0.0 milestone Dec 29, 2017
@wopian wopian changed the title Cannot post collections with relationships with length > 1 Serialising relationship arrays is not handled Dec 29, 2017
duk3luk3 added a commit to duk3luk3/kitsu that referenced this issue Dec 29, 2017
@duk3luk3
Copy link
Contributor Author

Is this the right approach to fix this? duk3luk3@3f6ea89

@wopian
Copy link
Owner

wopian commented Dec 29, 2017

Not quite. Will need to handle objects and arrays separately, since that check will only verify the first item in the array is valid and the contents of the if block will fail with arrays.

@duk3luk3
Copy link
Contributor Author

OK, I've backed out that commit and just added a spec for now so I can make sure I understand fully what needs to be changed (This is my first real JSONAPI rodeo).

I made a PR so I can run the tests without setting them up myself.

Please let me know if the spec needs fixing. It fails as expected in the appveyor build: https://ci.appveyor.com/project/wopian/kitsu/build/519/job/h8rp731s63chvols

duk3luk3 added a commit to duk3luk3/kitsu that referenced this issue Dec 30, 2017
duk3luk3 added a commit to duk3luk3/kitsu that referenced this issue Dec 30, 2017
duk3luk3 added a commit to duk3luk3/kitsu that referenced this issue Dec 30, 2017
duk3luk3 added a commit to duk3luk3/kitsu that referenced this issue Dec 30, 2017
@wopian wopian closed this as completed in 596ff89 Jan 1, 2018
@ghost ghost removed the in progress label Jan 1, 2018
@wopian wopian removed the help wanted label Jan 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants