data icon indicating copy to clipboard operation
data copied to clipboard

JSON:API Serializer returns null for records of unknown type (instead of ignoring them)

Open maxymczech opened this issue 4 years ago • 5 comments

Reproduction

Github repository with reproduction: https://github.com/maxymczech/ember-data-crash-test

Description

Whenever we add new model type to the API, the older versions of the app start crashing upon encountering new model types. For now, I was able to solve this issue by adding the following code to application serializer:

import JSONAPISerializer from '@ember-data/serializer/json-api';

export default class ApplicationSerializer extends JSONAPISerializer {
  normalizeFindAllResponse() {
    const payload = super.normalizeFindAllResponse(...arguments);
    return payload && {
      ...payload,
      data: (payload?.data || []).filter(Boolean)
    };
  }
}

Versions

-- [email protected]
-- [email protected]
-- [email protected]

maxymczech avatar Sep 21 '21 14:09 maxymczech

@maxymczech Your link got double-pasted in the OP here: https://github.com/maxymczech/ember-data-crash-test

jfdnc avatar Oct 01 '21 21:10 jfdnc

@jfdnc, sorry my bad, fixed, thank you.

maxymczech avatar Oct 01 '21 21:10 maxymczech

What's the error thrown?

runspired avatar Dec 13 '21 22:12 runspired

Error while processing route: index Cannot read properties of null (reading 'type') TypeError: Cannot read properties of null (reading 'type')
    at Store._pushInternalModel (http://localhost:4200/assets/vendor.js:89852:28)
    at http://localhost:4200/assets/vendor.js:89833:38
    at Backburner._run (http://localhost:4200/assets/vendor.js:57697:25)
    at Backburner._join (http://localhost:4200/assets/vendor.js:57673:21)
    at Backburner.join (http://localhost:4200/assets/vendor.js:57444:19)
    at Store._push (http://localhost:4200/assets/vendor.js:89818:52)
    at http://localhost:4200/assets/vendor.js:87303:34
    at invokeCallback (http://localhost:4200/assets/vendor.js:64509:17)
    at publish (http://localhost:4200/assets/vendor.js:64492:9)
    at http://localhost:4200/assets/vendor.js:58819:53

maxymczech avatar Dec 15 '21 00:12 maxymczech

@maxymczech ~~that's not an unknown model type, it seems you literally tried to supply null to the store as the data. I'd check that your serializer output is correct.~~

Edit: I see better now, the JSON:API serializer is returning an invalid response for those records.

runspired avatar Dec 15 '21 07:12 runspired