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

Fixed issue with recursion and passing strings into objects. #68

Merged
merged 1 commit into from May 21, 2015
Merged

Fixed issue with recursion and passing strings into objects. #68

merged 1 commit into from May 21, 2015

Conversation

1313
Copy link
Contributor

@1313 1313 commented Feb 13, 2015

I stumbled upon a bug in when using this merge method directly to merge two query param objects with each other containing only string values.

e.g, target as {userId: "1234"} source as {userId: "64321"}.

This recursively caused the logic for passing strings into objects kick in as follows:
Target = "1234", Source = "64321".
"1234"["64321"] = true;

Hence the modified else clause to be sure that target is an object and not a string.

Regards
Per

I stumbled upon a bug in when using this merge method directly to merge two query param objects with each other containing only string values. 

e.g, target as {userId: "1234"} source as {userId: "64321"}.

This recursively caused the logic for passing strings into objects kick in as follows:
Target = "1234", Source = "64321".
"1234"["64321"] = true;

Hence the modified else clause to be sure that target is an object and not a string.

Regards
Per
@nlf
Copy link
Collaborator

nlf commented Feb 13, 2015

Can you provide a test for this?

@nlf nlf added the bug label Feb 13, 2015
@nlf nlf self-assigned this Feb 13, 2015
@1313
Copy link
Contributor Author

1313 commented Feb 13, 2015

The issue is related to the react-router project which uses this module's merge method directly here. Which might not be a common use case and a bit against encapsulation.

As you only use the merge method within the parse module with one specific use case i don't think i will be able to reproduce the bug through the parse module.

I could however provide you with a test for the merge method itself if you like.

@1313
Copy link
Contributor Author

1313 commented Feb 13, 2015

Test case to trigger the bug.

var Code = require('code');
var Lab = require('lab');
var merge = require('../lib/utils').merge;

var lab = exports.lab = Lab.script();
var expect = Code.expect;
var describe = lab.experiment;
var it = lab.test;

describe('merge()', function () {
    it('merge two query objects', function (done) {
        var exisitingQuery = {userId: "12345"};
        var newUserQuery = {userId: "76432"};
        expect(merge(exisitingQuery, newUserQuery)).to.deep.equal(newUserQuery);
        done();
    });
});

@nlf
Copy link
Collaborator

nlf commented Feb 13, 2015

I think adding additional tests would be fine. The most important thing is that we have test coverage for this, otherwise it's all too easy for this to get broken again in the future. Go ahead and make a test/utils.js module and put the above test in it as part of this pull request. We can flesh it out further later.

@sdemjanenko
Copy link

I would like to see this accepted.

@nlf
Copy link
Collaborator

nlf commented Mar 13, 2015

I'm going to write some additional tests and get this published soon

@nlf nlf added this to the 3.0.0 milestone May 21, 2015
nlf added a commit that referenced this pull request May 21, 2015
Fixed issue with recursion and passing strings into objects.
@nlf nlf merged commit 23844dd into ljharb:master May 21, 2015
@nlf
Copy link
Collaborator

nlf commented May 21, 2015

Just FYI this wasn't correct behavior either, the expected result of merging { a: 'b' } and { a: 'c' } should be { a: ['b', 'c'] } which is what will happen in version 3.0.0 of this module

nlf added a commit that referenced this pull request May 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants