-
-
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
[BUGFIX release] Normalize attrs keys #3910
Conversation
Thanks for the PR @fsmanuel! Can you shed some light on why this would be needed?
This feels wrong to me. The first string is the name of your model's attribute, the second string is the name of the payload key (equivalent to By looking at the test an attribute named Would love to know more about the use-case needing this :) |
@wecc you are right, it feels wrong. The dasherized version is the current behavior. I wasn't sure if we want to break that behavior (obviously it is wrong but has been the only way). I'm happy to remove it and add an error if we detect dasherized attrs keys. |
@fsmanuel payload keys (attributes and relationships) with What's the problem this PR is trying to solve? Sorry if I'm missing something obvious here. |
As discussed with @wecc on Slack the intended behavior should be to check if the We can use something like: _attributesOrRelationshipsHas(typeClass, key) {
const has = get(typeClass, 'attributes').has(key) || get(typeClass, 'relationshipsByName').has(key);
Ember.assert('There is no attribute or relationship with the name `' + key + '`. Check your serializers attrs hash.', has);
return has;
} If you are ok with it. I'll update the PR later. |
@fsmanuel I think you can access the typeClass using |
Also |
Perfect. Will update the PR. |
@wecc updated. I also fixed my mistake that led to the RESTSerializer test failing. |
const hasAttributeKey = get(modelClass, 'attributes').has(key); | ||
const hasRelationshipKey = get(modelClass, 'relationshipsByName').has(key); | ||
|
||
Ember.assert('There is no attribute or relationship with the name `' + key + '` on `' + modelClass.modelName + '`. Check your serializers attrs hash.', hasAttributeKey || hasRelationshipKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think its possible for this assertion to get triggered since it looks like _getMappedKey
always gets called in a eachAttribute
or eachRelationship
loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind I see it being called in normalizeUsingDeclaredMapping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the code from line 762 and 763 into the assert so it will be stripped correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wecc good point.
Left a comment about the assert, other than that it looks good. |
if (!hash.hasOwnProperty(payloadKey)) { continue; } | ||
|
||
if (payloadKey !== key) { | ||
hash[key] = hash[payloadKey]; | ||
// we need to normalize for JSONAPISerializer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment should be removed as this is not really JSONAPISerializer
specific. You would encounter this issue with any serializer where you customize keyForAttribute
/keyForRelationship
to not just pass through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true!
updated. |
Thanks @fsmanuel do you mind squashing this pr into 1 commit? |
9e07cac
to
45e6db9
Compare
@bmac i never did that before but it worked ;) |
[BUGFIX release] Normalize attrs keys
Thanks @fsmanuel |
This change has caused our app to have a huge number of warnings now for seemingly unrelated models, that didn't occur in the 2.2.0.beta.3:
But import DS from 'ember-data';
export default DS.Model.extend({
email: DS.attr('string'),
super: DS.attr('boolean', { defaultValue() { return false; }}),
firstName: DS.attr('string'),
lastName: DS.attr('string')
}); My feeling is this is to do with active-model-adapter? |
@Soliah what does your serializer look like? I suspect you have |
@wecc ah right, we do have that in there. Thanks. |
The JSONAPISerializer expects
The PR fixes that and now both (dasherized and camelized) work
It also fixes #3879 but it breaks a test. I wonder if the test is intentional because it declares a relationship mapping that uses
keyForAttribute
to normalize it. I added a test for an attribute but I'm worried to remove the (wrong) test.I added two comments where I would add a deprecation if the
attrs
key includes a dash.@wecc @bmac can you review?