with ActiveModelSerializer
usage after the ember-data upgrade, the values of has_many case on the request payload had changed when the model has no records by relation, for example:
// app/models/user.js
import Model from 'ember-data/model';
import { hasMany } from 'ember-data/relationships';
export default Model.extend(Validations, {
posts: hasMany('post'),
...
}
// app/models/post.js
import Model from 'ember-data/model';
import { belongsTo } from 'ember-data/relationships';
export default Model.extend(Validations, {
user: belongsTo('user'),
...
}
so, if User has no posts, previously was sent []
into BE, but after ember-data
bump to 2.14.11 no key-values sent, for example: (request payload)
Before:
user: { id: 1, post_ids: [] }
After:
user: { id: 1 }
Note:
"active-model-adapter": "2.2.0"
"ember-data": "2.14.11"
It's caused by: https://github.com/emberjs/data/compare/v2.14.3...v2.14.4
workaround: app/serializers/application.js
import EmbeddedRecordsMixin from 'ember-data/serializers/embedded-records-mixin';
import { ActiveModelSerializer } from 'active-model-adapter';
import { pluralize } from 'ember-inflector';
export default ActiveModelSerializer.extend(EmbeddedRecordsMixin, {
isNewSerializerAPI: true,
serializeHasMany(snapshot, model, relation) {
this._super(...arguments);
let relationKey = `${pluralize(relation.key.underscore()).slice(0, -1)}_ids`;
if (relation.kind === 'hasMany' && !model[relationKey]) {
model[relationKey] = Object.freeze([]);
}
}
});