emberjs / data

A lightweight reactive data library for web applications. Designed over composable primitives.
https://api.emberjs.com/ember-data/release
Other
3.03k stars 1.33k forks source link

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

Open maxymczech opened 3 years ago

maxymczech commented 3 years ago

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

-- ember-source@3.26.1
-- ember-cli@3.26.1
-- ember-data@3.26.0
jfdnc commented 3 years ago

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

maxymczech commented 3 years ago

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

runspired commented 2 years ago

What's the error thrown?

maxymczech commented 2 years ago
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
runspired commented 2 years ago

@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.