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

[BUGFIX beta] Ember.get with first part of path chain resolving to null returns null rather than undefined #13449

Merged
merged 1 commit into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ember-metal/lib/property_get.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function _getPath(root, path) {

for (let i = 0; i < parts.length; i++) {
if (obj == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only want to call get on object like things, this should do if (!isObjectLike(obj)) { return undefined; }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be specific, I would check falsy, and that the typeof was 'string', 'object', 'function' but at a minimum this can simply check falsy. I'm sure the obj == null check was a bad fix to this sometimes returning false vs undefined.

Copy link
Member

@mmun mmun May 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falsey alone won't work because of empty strings, e.g.

Ember.get({ foo: "" }, 'foo.length') should be 0 not undefined. (Can you add a test for this one @robbiepitts? if there isn't one already)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm, using get for string.length seems super lame.

return obj;
return undefined;
}

obj = get(obj, parts[i]);
Expand Down
13 changes: 9 additions & 4 deletions packages/ember-metal/tests/accessors/get_path_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var moduleOpts = {
emptyString: '',
Wuz: {
nar: 'foo'
}
},
nullValue: null
};
},

Expand Down Expand Up @@ -46,13 +47,17 @@ QUnit.test('[obj, foothis.bar] -> obj.foothis.bar', function() {
});

QUnit.test('[obj, falseValue.notDefined] -> (undefined)', function() {
equal(get(obj, 'falseValue.notDefined'), undefined);
strictEqual(get(obj, 'falseValue.notDefined'), undefined);
});

QUnit.test('[obj, emptyString.length] -> 0', function() {
equal(get(obj, 'emptyString.length'), 0);
});

QUnit.test('[obj, nullValue.notDefined] -> (undefined)', function() {
strictEqual(get(obj, 'nullValue.notDefined'), undefined);
});

// ..........................................................
// GLOBAL PATHS TREATED LOCAL WITH GET
//
Expand All @@ -66,9 +71,9 @@ QUnit.test('[obj, Wuz.nar] -> obj.Wuz.nar', function() {
});

QUnit.test('[obj, Foo] -> (undefined)', function() {
equal(get(obj, 'Foo'), undefined);
strictEqual(get(obj, 'Foo'), undefined);
});

QUnit.test('[obj, Foo.bar] -> (undefined)', function() {
equal(get(obj, 'Foo.bar'), undefined);
strictEqual(get(obj, 'Foo.bar'), undefined);
});
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ QUnit.test('should return null when property value is null on Ember.Observable',
});

QUnit.test('should call unknownProperty when value is undefined on Ember.Observable', function() {
equal(get(object, 'unknown'), 'unknown');
equal(object.lastUnknownProperty, 'unknown');
equal(get(objectA, 'unknown'), 'unknown');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would fail if only the Ember.get() module was run and relied on the object.get() module being run first to pass—for obvious reasons.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. Good catch!

equal(objectA.lastUnknownProperty, 'unknown');
});

QUnit.test('should get normal properties on standard objects', function() {
Expand Down