Skip to content

Don't serialize new has many relationships - #5324

Merged
runspired merged 1 commit into
warp-drive-data:masterfrom
ryanto:dont_serialize_new_has_many
Apr 4, 2018
Merged

Don't serialize new has many relationships#5324
runspired merged 1 commit into
warp-drive-data:masterfrom
ryanto:dont_serialize_new_has_many

Conversation

@ryanto

@ryanto ryanto commented Jan 12, 2018

Copy link
Copy Markdown
Contributor

Similar to #5317 but for hasMany.

Currently when saving a model with an unsaved hasMany a null id is used in the JSON:API document.

For example

let company = this.store.peekRecord('company', 1);

let user = this.store.createRecord('user');
company.get('users').addObject(user);

company.save();

Generates this post payload (note that we're using serializer option attrs.users.serialize = true)

{
  data: {
    type: 'company',
    id: '1',
    relationships: {
      users: [
        data: {
          type: 'users',
          id: null
        }
      ]
    }
  }
}

This is an invalid payload. To fix this, we'll filter out new records from using in the serialized belongsTo.

@ryanto
ryanto force-pushed the dont_serialize_new_has_many branch from a2841d6 to 111c0d6 Compare January 12, 2018 20:39

@bmac bmac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@sly7-7

sly7-7 commented Mar 9, 2018

Copy link
Copy Markdown
Contributor

Looks good if we don't want to automatically save the new record.
I just wonder in that scenario if we could add an option to send the newly created user together with the company. Not sure it that even makes sense.

@ryanto

ryanto commented Mar 9, 2018

Copy link
Copy Markdown
Contributor Author

@sly7-7 Yup! I think you're describing a common use case for most applications would want. I'm expecting JSONAPI operations to be the answer to that problem: json-api/json-api#1254

@runspired

Copy link
Copy Markdown
Contributor

@ryanto not super sure why this failed CI. Could you rebase and let it kick off again?

@ryanto
ryanto force-pushed the dont_serialize_new_has_many branch from 111c0d6 to 484e3a6 Compare April 4, 2018 14:32
@ryanto

ryanto commented Apr 4, 2018

Copy link
Copy Markdown
Contributor Author

All set, I'll keep an eye on CI.

FYI when I pulled the latest and ran yarn install it looks like it added an entry to the lockfile.

@runspired

Copy link
Copy Markdown
Contributor

victim of a timeout, restarted tests again

@runspired
runspired merged commit f3946fb into warp-drive-data:master Apr 4, 2018
@runspired

Copy link
Copy Markdown
Contributor

Thanks @ryanto !

@juggy

juggy commented May 2, 2018

Copy link
Copy Markdown
Contributor

There is problem with this bug fix. An empty relationship is not serialized anymore, which is a different behavior than before. @runspired @ryanto

If you have a relation and remove it and save the record, to inform the backend of that relation change, you need to send that empty array back.

@ryanto

ryanto commented May 2, 2018

Copy link
Copy Markdown
Contributor Author

@juggy Yikes! Sorry about that.

Could you give me a failing test case, or even some example HTTP request/responses with what you expect vs what Ember Data is doing?

@juggy

juggy commented May 2, 2018

Copy link
Copy Markdown
Contributor

Well the format should be the one of 3.1...

...
"relationships": {
  "my_relations: {
    "data": []
  }
}

@ryanto

ryanto commented May 5, 2018

Copy link
Copy Markdown
Contributor Author

PR is here: #5466

@ryanto

ryanto commented May 8, 2018

Copy link
Copy Markdown
Contributor Author

@juggy A fix has been merged, let me know if you run into any other issues.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants