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

Create record with new hasMany relationship sends empty ids instead of attributes #3953

Closed
andruby opened this issue Nov 25, 2015 · 3 comments

Comments

@andruby
Copy link

andruby commented Nov 25, 2015

With this Book model:

export default DS.Model.extend({
  title: DS.attr('string'),
  authors: DS.hasMany('author')
});

When creating a record with new authors.

var book = this.store.createRecord('book', {title: 'The Ember Way'});
book.get('authors').createRecord({name: 'Jack'});
book.save();

The author relationships are serialised as if they were already persisted. The relationship is serialised as a Resource Identifier Object.

{
  "data":{
    "attributes":{
      "title":"The Ember Way"
    },
    "relationships":{
      "authors":{
        "data":[
          {"type":"author", "id":null}
        ]
      }
    },
    "type":"book"
  }
}

From my understanding of JSONAPI, the unpersisted relationship should be serialised as a Resource Object with attributes.

{
  "data":{
    "attributes":{
      "title":"The Ember Way"
    },
    "relationships":{
      "authors":{
        "data":[
          {
            "attributes":{
              "name":"Jack"
            },
            "type":"author"
          }
        ]
      }
    },
    "type":"book"
  }
}
@andruby andruby changed the title Create record with new hasMany relationship does not follow JSONAPI spec Create record with new hasMany relationship sends empty ids instead of attributes Nov 25, 2015
@wecc
Copy link
Contributor

wecc commented Nov 25, 2015

The author relationships are serialised as if they were already persisted

This looks like a bug to me. We wouldn't want to serialize new records before they have been saved. Not 100% sure what the correct path here would be.

From my understanding of JSONAPI, the unpersisted relationship should be serialised as a Resource Object with attributes.

Hm, I don't think "embedded" records in relationships are a thing in JSON API at all? Unsure though. Can you point me to the part of the spec that made you think this is the recommended behavior?

@andruby
Copy link
Author

andruby commented Nov 26, 2015

@wecc I think you are right. The JSONAPI spec does not explicitly support what I'm trying to do. It seems natural though, and need it to atomically create a new record with relationships. I'll go on the jsonapi discuss to see if they are looking at extending the spec for these kinds of requests. Thanks!

@andruby
Copy link
Author

andruby commented Nov 26, 2015

@wecc Ember-Data already supports serializing relationships through the EmbeddedRecordsMixin. Thanks to @lolmaus at json-api/json-api#795 (comment)

Adding the EmbeddedRecordsMixin was as easy as creating a custom serializer for Book from my example:

import DS from 'ember-data'
import ApplicationSerializer from 'face/application/serializer'

export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    author: { embedded: 'always' }
  }
})

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

No branches or pull requests

2 participants