adopted-ember-addons / ember-changeset

Ember.js flavored changesets, inspired by Ecto
http://bit.ly/ember-changeset-demo
MIT License
431 stars 141 forks source link

Cast seems to remove multi level properties like address.country, even if they are included in the array passed to the cast function #667

Open andrew-paterson opened 1 year ago

andrew-paterson commented 1 year ago

Version

ember-changeset 4.1.1 ember-source 3.28

Test Case

Reduced test case here

Steps to reproduce

If running the app in the reduced test case above, simply click the button "Test" on the landing page of the above app and look at the console output.

Doing so runs this action, closely based on the docs for cast, so you could also just copy and paste this code into a component.

export default class TestComponent extends Component {
  @action
  test() {
    let user = { age: 21, address: { zipCode: '10001' } };
    let changeset = Changeset(user);
    let allowed = ['name', 'address.country'];
    console.log(`changeset.set('name', 'Jim Bob');`);
    changeset.set('name', 'Jim Bob');
    console.log(`changeset.set('address.country', 'United States');`);
    changeset.set('address.country', 'United States');
    console.log(`changeset.set('unwantedProp', 'foo');`);
    changeset.set('unwantedProp', 'foo');
    console.log(`changeset.set('address.unwantedProp', 123);`);
    changeset.set('address.unwantedProp', 123);
    console.log(`changeset.get('name // ${changeset.get('name')}`);
    console.log(`changeset.get('address.country // ${changeset.get('address.country')}`);
    console.log(`changeset.get('unwantedProp')); // ${changeset.get('unwantedProp')}`);
    console.log(`changeset.get('address.unwantedProp // ${changeset.get('address.unwantedProp')}`);
    changeset.cast(allowed); // returns changeset
    console.log(`>>>> changeset.cast([${allowed}])`);
    console.log(`changeset.get('name // ${changeset.get('name')}`);
    console.log(`changeset.get('address.country // ${changeset.get('address.country')}`);
    console.log(`changeset.get('unwantedProp')); // ${changeset.get('unwantedProp')}`);
    console.log(`changeset.get('another.unwantedProp // ${changeset.get('another.unwantedProp')}`);
    console.log(changeset.get('another.unwantedProp')); // undefined
  }
}

Expected Behavior

Logging changeset.get('address.country') aftercastshould beUnited States`.

changeset.set('name', 'Jim Bob');
changeset.set('address.country', 'United States');
changeset.set('unwantedProp', 'foo');
changeset.set('address.unwantedProp', 123);
changeset.get('name // Jim Bob
changeset.get('address.country // United States
changeset.get('unwantedProp')); // foo
changeset.get('address.unwantedProp // 123
>>>> changeset.cast([name,address.country])
changeset.get('name // Jim Bob
changeset.get('address.country // United States
changeset.get('unwantedProp')); // undefined
changeset.get('another.unwantedProp // undefined

Actual Behavior

Logging changeset.get('address.country') aftercastis not undefined, where it should beUnited States. Note that beforecastit logs asUnited States. Note also that the same issue does not occur forname`.

changeset.set('name', 'Jim Bob');
changeset.set('address.country', 'United States');
changeset.set('unwantedProp', 'foo');
changeset.set('address.unwantedProp', 123);
changeset.get('name // Jim Bob
changeset.get('address.country // United States
changeset.get('unwantedProp')); // foo
changeset.get('address.unwantedProp // 123
>>>> changeset.cast([name,address.country])
changeset.get('name // Jim Bob
changeset.get('address.country // undefined
changeset.get('unwantedProp')); // undefined
changeset.get('another.unwantedProp // undefined