Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when upgrading from 3.24 to 3.28 when a model has an isValid property #7897

Closed
kategengler opened this issue Feb 23, 2022 · 2 comments
Closed
Labels
🏷️ bug This PR primarily fixes a reported issue

Comments

@kategengler
Copy link
Member

When upgrading an app from 3.24 to 3.28, I saw an error:

Uncaught TypeError: Cannot read properties of undefined (reading 'isValid')
    at Model.get isValid (vendor.js:99707:32)
    at vendor.js:55315:29
    at track (vendor.js:76227:7)
    at Model.desc.get (vendor.js:55314:40)
    at mergeProps (vendor.js:32534:40)
    at mergeMixins (vendor.js:32496:11)
    at mergeMixins (vendor.js:32498:11)
    at applyMixin (vendor.js:32643:5)
    at Mixin.apply (vendor.js:32889:14)
    at Function.proto (vendor.js:46713:31)
  | eachComputedProperty | @ | vendor.js:46670
  | get relationshipsObject | @ | vendor.js:100840
  | desc.get | @ | vendor.js:99473
  | get relationshipsByName | @ | vendor.js:100825
  | desc.get | @ | vendor.js:99473
  | eachRelationship | @ | vendor.js:100909
  | (anonymous) | @ | vendor.js:128380
  | discoverEmberDataModels | @ | vendor.js:128377

(For anyone searching -- in tests, without No try/catch checked this presented as mirage failing to start).

I tracked this down to an isValid computed property defined in Ember classic syntax on a model.

import Model from '@ember-data/model';
import { computed } from '@ember/object';

export default Model.extend({
  isValid: computed('...
});

Renaming the computed property from isValid to anything else removes the error. I have no problem just renaming, but was surprised that the user-defined isValid interfered with something called by ember-data.

@runspired
Copy link
Contributor

runspired commented Feb 25, 2022

So it appears the issue here is that mirage looks up models and accesses the static properties on them without using the container. This has a number of ill-effects and was never officially supported though it is something we felt we need to explicitly deprecate. The deprecation RFC is here: emberjs/rfcs#741 and this second RFC was also motivated by the sort of bug discovered here. emberjs/rfcs#738

All this to say that I'm not surprised that this lookup results in mergeMixins being called since the proto chain must be realized when a static property or method access occurs, and I am also not surprised that this merging does a read access of isValid during the merge (this is a bug I previously surfaced to @pzuraq)

Prior reading on this and a related bug in Ember:

BUT! While I'm not surprised at the pathology of this triggering, it does mean we had a blind spot around it being able to be triggered by collisions with the properties that are on Model out of the box. Those properties are intended to be overridable and not being so would even harm the implementation of the Links & Attrs RFC that @mansona is currently working on in #7855

I built this twiddle to show that native class syntax does not have this issue but classic syntax does. This means that folks who hit this bug can resolve it by refactoring to native syntax

import Model, { attr } from '@ember-data/model';

class User extends Model {
  @attr isValid;
}
/*
// replace the class definition with this one to trigger the error
const User = Model.extend({
  isValid: attr(),
});
*/

const attrs = User.attributes;
console.log(attrs);

export default User;

@kategengler
Copy link
Member Author

Since it can also be worked around by changing the attribute name (if not intentionally overriding) and native-class syntax is the way-forward, I'm comfortable closing this issue (but will leave it open for you to do so if you agree).

Thanks for looking into this and writing it up so clearly!

@runspired runspired added 🏷️ bug This PR primarily fixes a reported issue and removed Bug labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bug This PR primarily fixes a reported issue
Projects
None yet
Development

No branches or pull requests

3 participants