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] extract polymorphic belongsTo in RESTSerializer #3835

Merged
merged 2 commits into from
Oct 20, 2015
Merged

[BUGFIX] extract polymorphic belongsTo in RESTSerializer #3835

merged 2 commits into from
Oct 20, 2015

Conversation

pangratz
Copy link
Member

@pangratz pangratz commented Oct 9, 2015

This change adds the correct extraction of a polymorphic belongsTo
specified in the payload in the following form:

{
  id: 123,
  // ...
  message: 1,
  messageType: 'post'
}

where the model is specified as:

DS.Model.extend({
  message: DS.belongsTo('message', { polymorphic: true })
})

If this gets accepted, this should likely be ported back to 1.13 as well.

This closes #3806

// corresponding JSON-API representation. The former case is handled within
// the super class (JSONSerializer).
var isPolymorphic = relationshipMeta.options.polymorphic;
var typeProperty = `${relationshipKey}Type`;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should probably go through keyForAttribute. I think we had a bug related to this previously. It would also make the ActiveModelSerializer work out of the box... I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

relationshipKey is the result of this.keyForRelationship(key, relationshipMeta.kind, 'deserialize'); from this line.

In serializePolymorphicType above in line 686 keyForAttribute is used, so using keyForAttribute here would make it consistent I guess. But judging by the name, wouldn't be keyForRelationship the correct one, for both serializePolymorphicType and the new serializePolymorphicType?

It looks like AMS uses `keyForRelationship ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, keyForRelationship makes more sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to make sure, my intention here was to do keyForAttribute keyForRelationship on the typeProperty string.

Even though relationshipKey is cased correctly that doesn't mean we can just append Type to the end of it.

Say you have a bestFriend: belongsTo() and you've overriden keyForRelationship to returned dasherized. bestFriend is now best-friend. Odds are your payload contains best-friend-type, not best-friendType.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good point @wecc.

I will add a keyForPolymorphicType hook, which will also be used here...

@bmac
Copy link
Member

bmac commented Oct 16, 2015

@pangratz could you update the commit message to start with [BUGFIX beta]?

});

typeKey = key;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

The current way to get the myKeyType string for the myKey relationship is using keyForAttribute which is not correct IMHO. This is changed in the course of this PR by adding a keyForPolymorphicType hook.

This could break current behavior, that's why a deprecation message is added. I will add a test for the deprecation if the deprecation is found to be necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I think this is save without the deprecation, since there wouldn't have been another way to change the way the polymorphic record is serialized except for overriding serializePolymorphicType as a whole...

Copy link
Member

Choose a reason for hiding this comment

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

@pangratz it looks like a user can't use keyForPolymorphicType if it differs from ${key}Type; since key always wins. Is that correct? Is there anyway we could rewrite this logic so uses who define a custom keyForPolymorphicType get the new happy path without a deprecation warning?

Copy link
Member Author

Choose a reason for hiding this comment

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

Uff, you're absolutely right. Nice catch! Lol, logic, how do you even work. I will change the code so the deprecation warning is only logged when keyForPolymorphicType is not overwritten...

This change adds the correct extraction of a polymorphic belongsTo
specified in the payload in the following form:

``` js
{
  id: 123,
  // ...
  message: 1,
  messageType: 'post'
}
```

where the model is specified as:

``` js
DS.Model.extend({
  message: DS.belongsTo('message', { polymorphic: true })
})
```

---

This commit introduces a new hook `keyForPolymorphicType` which can be
overwritten to customize the key which holds the polymorphic type.
Currently `keyForAttribute` is used to get the key under which the type
of a polymorphic record is serialized. This is not correct, as the new
`keyForPolymorphicType` hook should be used.

This commit uses the new hook and falls back to the previous vesion, if
the key generated via the old method (keyForAttribute) is different to
the new version and the `keyForPolymorphicType` has not been
overwritten. A deprecation warning is logged if this is the case.
@pangratz
Copy link
Member Author

@bmac I've updated the logic and added a check which ignores the fallback when the keyForPolymorphicType has been overwritten.

bmac added a commit that referenced this pull request Oct 20, 2015
[BUGFIX] extract polymorphic belongsTo in RESTSerializer
@bmac bmac merged commit 4e83729 into emberjs:master Oct 20, 2015
@bmac
Copy link
Member

bmac commented Oct 20, 2015

Thanks @pangratz.

@pangratz pangratz deleted the extract-polymorphic-belongs-to branch October 20, 2015 21:09
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.

Polymorphic belongs-to relation model is not serialized in Ember-data v1.13
3 participants