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

[BUGFIX] EmbeddedRecordMixin should include the type serializing hasMany as ids #3848

Merged

Conversation

cibernox
Copy link
Contributor

It is better explained with an example.

Lets say that that you have an error that contains many attachable items
(attachables: DS.hasMany(‘attachable’, { polymorphic: true })) and the serializer looks like this:

export default ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    attachables: {
      serialize: 'ids',
      deserialize: 'ids'
    }
  }
});

At the moment the serialized json contains attachables: [1,2,3, ...] but that is useless for polymorphic relationships, the serialized array should contain {id, type} tuples (attachables: [{type: 'pdf', id: 1}, {type: 'xls', id: 1}])

@bmac
Copy link
Member

bmac commented Dec 3, 2015

@cibernox I don't think we can merge this as is without potentially breaking backwards compatibility. It would be better if there were a flag to opt into this behavior, possibly changing serialize: 'ids', to something like serialize: 'ids-and-types',.

@cibernox
Copy link
Contributor Author

cibernox commented Dec 3, 2015

I completely forgot about this PR 😓

Then, would you the accept then a 3rd serializing strategy called ids-and-types? I don't remember the details but I think that this wouldn't be too hard to implement.

@bmac
Copy link
Member

bmac commented Dec 4, 2015

I think we would accept a 3rd serializing strategy called ids-and-types (feel free to suggest a better name) in the EmbeddedRecordMixin.

@cibernox
Copy link
Contributor Author

cibernox commented Dec 4, 2015

Nahh, that name is as explicit at is can possibly be. I'll try to allocate some time over the weekend for this.

@igorT
Copy link
Member

igorT commented Dec 21, 2015

@cibernox ping?

@cibernox cibernox force-pushed the fix_bug_serializing_polymorphic_has_many_as_ids branch 2 times, most recently from 5c8abe8 to 90c0ae7 Compare December 24, 2015 16:11
@cibernox
Copy link
Contributor Author

Santa Claus left this PR under the tree 👍

@cibernox
Copy link
Contributor Author

@igorT any comments on this PR? I will have some free time this weekend in case something needs to be changed

@bmac
Copy link
Member

bmac commented Jan 3, 2016

@cibernox do you mind wrapping the changes in this pr in a feature flag?

@cibernox cibernox force-pushed the fix_bug_serializing_polymorphic_has_many_as_ids branch from 90c0ae7 to c191b7d Compare January 3, 2016 23:17
@cibernox
Copy link
Contributor Author

cibernox commented Jan 3, 2016

Flagged under ds-serialize-ids-and-types.
However, what happens with the doc comments I've updated? If the API docs is generated from those it might mislead users. Should I remove them?

```

This is particularly useful for polymorphic relationships not backed by STI when just including the id
of the records is not enought.
Copy link
Member

Choose a reason for hiding this comment

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

s/enought/enough/

@pangratz
Copy link
Member

pangratz commented Jan 4, 2016

However, what happens with the doc comments I've updated? If the API docs is generated from those it might mislead users. Should I remove them?

Maybe a solution would be to mention in the comment that the functionality is only available when the ds-serialize-ids-and-types feature is enabled? Not sure if this is less confusing though...

@cibernox cibernox force-pushed the fix_bug_serializing_polymorphic_has_many_as_ids branch from c191b7d to 567ef9f Compare January 4, 2016 00:55
@cibernox
Copy link
Contributor Author

cibernox commented Jan 4, 2016

Applied feedback, and also tagged the commit as [FEATURE ds-serialize-ids-and-types]

@cibernox
Copy link
Contributor Author

ping?

@cibernox cibernox force-pushed the fix_bug_serializing_polymorphic_has_many_as_ids branch 2 times, most recently from 0f38e57 to 550ec55 Compare February 4, 2016 16:47
@cibernox
Copy link
Contributor Author

cibernox commented Feb 4, 2016

Rebased and green.

@bmac
Copy link
Member

bmac commented Feb 4, 2016

cc @igorT we should look at this tomorrow.

…edding strategy for non STI polymorphic hasMany

Taking as example this scenario:

```js
User = DS.Model.extend({
  name:    DS.attr('string'),
  pets: DS.hasMany('pet', { polymorphic: true })
});

Pet = DS.Model.extend({
  name: DS.attr('string'),
});

Cat = Pet.extend({
  // ...
});

Parrot = Pet.extend({
  // ...
});
```

As of today, when using the `DS.EmbeddedRecordsMixin` in a serializer and configuring
the serialization stategy like this:

```
attrs: {
  pets: { serialize: 'ids' }
}
```

The relationship is serialized:

```json
    {
      "user": {
        "id": "1"
        "name": "Bertin Osborne",
        "pets": [1,2]
      }
    }
```

This works ok if the polymorphism is based on STI, but that is just one specific implementation
does not cover all use cases. Probably when a hasMany relationship is polymorphic the serialization
should generate an array of object containing `id` and `type` by default, but at this point this
can't be changed because it would break apps in the wild.

Because of that a new serialization strategy named `ids-and-types` has been created that covers this
use case.

Probably this should become the default behavior of the `ids` strategy in ember 3.0, but not for now.

For the same example above, this stragegy would generate the following payload:

    ```js
    {
      "user": {
        "id": "1"
        "name": "Bertin Osborne",
        "pets": [
          { "id": "1", "type": "Cat" },
          { "id": "2", "type": "Parrot"}
        ]
      }
    }
    ```

Note that with this strategy if the differenty type of records don't whare the same ids space,
that is not a problem.

    ```js
    {
      "user": {
        "id": "1"
        "name": "Bertin Osborne",
        "pets": [
          { "id": "1", "type": "Cat" },
          { "id": "1", "type": "Parrot"} // Same id, but different type
        ]
      }
    }
    ```
@cibernox cibernox force-pushed the fix_bug_serializing_polymorphic_has_many_as_ids branch from 550ec55 to 7989453 Compare February 25, 2016 20:19
@cibernox
Copy link
Contributor Author

Rebased.

bmac added a commit that referenced this pull request Feb 26, 2016
…c_has_many_as_ids

[BUGFIX] EmbeddedRecordMixin should include the type serializing hasMany as ids
@bmac bmac merged commit 3f6d3a8 into emberjs:master Feb 26, 2016
@cibernox cibernox deleted the fix_bug_serializing_polymorphic_has_many_as_ids branch February 26, 2016 14:26
@cibernox
Copy link
Contributor Author

👍

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.

None yet

4 participants