Fixed Ember Data deprecation warnings for `queryRecord` response returning array

no issue

- our API always returns an array whether we're performing a browse or find request but Ember Data expects explicit find requests to return a single object and throws deprecations when it sees an array
  - https://deprecations.emberjs.com/ember-data/v2.x/#toc_store-queryrecord-array-response-with-restserializer
- we previously had `normalizeSingleResponse` overrides in specific models that we use with `queryRecord` but we've since introduced `queryRecord` usage on more models but the associated "fix" was not duplicated in the serializers for those models leading to many deprecation warnings logged to the console in development and when testing
- moved the fix to the application serializer so it applies to all models
  - explicitly excluded `setting` model because that's a special-case and has it's own array-into-object serialization to represent multiple settings records as a single model instance
This commit is contained in:
Kevin Ansfield 2022-02-10 14:50:58 +00:00
parent d66d3e484c
commit 9768476a02
3 changed files with 12 additions and 29 deletions

View File

@ -49,4 +49,16 @@ export default class Application extends RESTSerializer {
return transform(key);
}
normalizeQueryRecordResponse(store, primaryModelClass, payload) {
const root = this.keyForAttribute(primaryModelClass.modelName);
const pluralizedRoot = pluralize(root);
if (payload[pluralizedRoot] && root !== 'setting') {
payload[root] = payload[pluralizedRoot][0];
delete payload[pluralizedRoot];
}
return super.normalizeQueryRecordResponse(...arguments);
}
}

View File

@ -1,7 +1,6 @@
/* eslint-disable camelcase */
import ApplicationSerializer from 'ghost-admin/serializers/application';
import {EmbeddedRecordsMixin} from '@ember-data/serializer/rest';
import {pluralize} from 'ember-inflector';
export default class PostSerializer extends ApplicationSerializer.extend(EmbeddedRecordsMixin) {
// settings for the EmbeddedRecordsMixin.
@ -14,22 +13,6 @@ export default class PostSerializer extends ApplicationSerializer.extend(Embedde
email: {embedded: 'always'}
};
normalizeSingleResponse(store, primaryModelClass, payload) {
let root = this.keyForAttribute(primaryModelClass.modelName);
let pluralizedRoot = pluralize(primaryModelClass.modelName);
if (payload[pluralizedRoot]) {
payload[root] = payload[pluralizedRoot][0];
delete payload[pluralizedRoot];
}
return super.normalizeSingleResponse(...arguments);
}
normalizeArrayResponse() {
return super.normalizeArrayResponse(...arguments);
}
serialize(/*snapshot, options*/) {
let json = super.serialize(...arguments);

View File

@ -19,16 +19,4 @@ export default class UserSerializer extends ApplicationSerializer.extend(Embedde
return super.extractSingle(...arguments);
}
normalizeSingleResponse(store, primaryModelClass, payload) {
let root = this.keyForAttribute(primaryModelClass.modelName);
let pluralizedRoot = pluralize(primaryModelClass.modelName);
if (payload[pluralizedRoot]) {
payload[root] = payload[pluralizedRoot][0];
delete payload[pluralizedRoot];
}
return super.normalizeSingleResponse(...arguments);
}
}