-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
attrs
not working for json-api-serializer
#3810
Comments
@digabit If your server is sending the key as export default DS.JSONAPISerializer.extend({
attrs: {
productNumber: { key: 'partNumber' }
}
}); |
Or maybe you need to do something like: import DS from 'ember-data';
const { JSONAPISerializer } = DS;
export default JSONAPISerializer.extend({
keyForAttribute(attr) {
if (attr === 'partNumber') {
attr = 'productNumber';
}
return this._super(...arguments);
}
}); |
Thanks for the suggestions. Your first idea doesn't seem to work. Your second suggestion does give another way to do it although you need to: return this._super(attr); However what you are proposing are work-arounds. This is exactly what the |
@digabit I think you should open a PR so this is fixed in the this.normalizeUsingDeclaredMapping(modelClass, resourceHash.attributes);
this.normalizeUsingDeclaredMapping(modelClass, resourceHash.relationships); There should likely be checks added, since |
@pangraz Ah yeah that is cleaner than overriding the whole method. Alright I'll put together a PR. |
@digabit my bad. I misunderstood. I didn't realize |
@HeroicEric No problem! |
Hey @digabit, kindly asking if you had any chance yet for the PR? |
I believe this issue is solved by #3847. |
Defining an
attrs
map when using JSONAPISerializer does not seem to be working.For example if I have the following model:
and the following serializer:
and JSON:API data like this coming over the wire:
The serializer does not assign '3004-787' to the
productNumber
property of the newproduct
.The root cause seems to be in
json-api-serializer
which usesnormalizeUsingDeclaredMapping
fromjson-serializer
. That method looks for the keys onattrs
at the root level of thehash
. In JSON:API these properties are inside theattributes
property. So it seems like we need to overridenormalizeUsingDeclaredMapping
and do something like this:This does in fact solve the problem in my environment. I didn't submit this as a pull request because I think there are probably other areas which I'm not familliar with but which need to be fixed as well. Like when serializing this property we need to do the reverse to construct the proper JSON from the model. Also does
attrs
apply to relationship names as well? Seems like it might have the same problem. ThanksThe text was updated successfully, but these errors were encountered: