From 9a24be2b4f1a59c8c345faf7c845da5a4bab177c Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Thu, 10 Feb 2022 14:50:58 +0000 Subject: [PATCH] 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 --- ghost/admin/app/serializers/application.js | 12 ++++++++++++ ghost/admin/app/serializers/post.js | 17 ----------------- ghost/admin/app/serializers/user.js | 12 ------------ 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/ghost/admin/app/serializers/application.js b/ghost/admin/app/serializers/application.js index 304bc631da..93f0085000 100644 --- a/ghost/admin/app/serializers/application.js +++ b/ghost/admin/app/serializers/application.js @@ -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); + } } diff --git a/ghost/admin/app/serializers/post.js b/ghost/admin/app/serializers/post.js index 0fe66ccc73..7c879d5acd 100644 --- a/ghost/admin/app/serializers/post.js +++ b/ghost/admin/app/serializers/post.js @@ -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); diff --git a/ghost/admin/app/serializers/user.js b/ghost/admin/app/serializers/user.js index 31077d9a2a..04bfd38a43 100644 --- a/ghost/admin/app/serializers/user.js +++ b/ghost/admin/app/serializers/user.js @@ -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); - } }