-
-
Notifications
You must be signed in to change notification settings - Fork 616
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
fix: correctly select properties of primitives #1279
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,14 +19,16 @@ describe('selectFields()', () => { | |
const instances = selectFields(req, ['foo', 'baz'], ['cookies']); | ||
|
||
expect(instances).toHaveLength(2); | ||
expect(instances[0]).toMatchObject({ | ||
expect(instances[0]).toEqual({ | ||
location: 'cookies', | ||
path: 'foo', | ||
originalPath: 'foo', | ||
value: 'bar', | ||
}); | ||
expect(instances[1]).toMatchObject({ | ||
expect(instances[1]).toEqual({ | ||
location: 'cookies', | ||
path: 'baz', | ||
originalPath: 'baz', | ||
value: 'qux', | ||
}); | ||
}); | ||
|
@@ -113,14 +115,19 @@ describe('selectFields()', () => { | |
const req = { | ||
query: { foo: ['bar', 'baz'] }, | ||
}; | ||
const instances = selectFields(req, ['foo[1]'], ['query']); | ||
const instances = selectFields(req, ['foo[1]', 'foo[2]'], ['query']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OOC, does this cover anything non-obvious, or is just for thoroughness? |
||
|
||
expect(instances).toHaveLength(1); | ||
expect(instances).toHaveLength(2); | ||
expect(instances[0]).toMatchObject({ | ||
location: 'query', | ||
path: 'foo[1]', | ||
value: 'baz', | ||
}); | ||
expect(instances[1]).toMatchObject({ | ||
location: 'query', | ||
path: 'foo[2]', | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
it('selects from headers using lowercase', () => { | ||
|
@@ -154,7 +161,7 @@ describe('selectFields()', () => { | |
}); | ||
|
||
it('selects inexistent properties', () => { | ||
const instances = selectFields({}, ['foo.bar.baz'], ['cookies']); | ||
const instances = selectFields({ cookies: {} }, ['foo.bar.baz'], ['cookies']); | ||
|
||
expect(instances).toHaveLength(1); | ||
expect(instances[0]).toEqual({ | ||
|
@@ -165,14 +172,24 @@ describe('selectFields()', () => { | |
}); | ||
}); | ||
|
||
it('does not select properties of primitives', () => { | ||
it('selects properties of primitives', () => { | ||
const req = { | ||
body: { foo: 1 }, | ||
}; | ||
const instances = selectFields(req, ['foo.toFixed'], ['body']); | ||
expect(instances).toHaveLength(0); | ||
}); | ||
const instances = selectFields(req, ['foo.toFixed', 'foo.nop'], ['body']); | ||
|
||
expect(instances).toHaveLength(2); | ||
expect(instances[0]).toMatchObject({ | ||
location: 'body', | ||
path: 'foo.toFixed', | ||
value: expect.any(Function), | ||
}); | ||
expect(instances[1]).toMatchObject({ | ||
location: 'body', | ||
path: 'foo.nop', | ||
value: undefined, | ||
}); | ||
}); | ||
it('deduplicates field instances', () => { | ||
const req = { | ||
body: { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -52,13 +52,19 @@ function expandPath(object: any, path: string | string[], currPath: readonly str | |||||
const rest = segments.slice(1); | ||||||
|
||||||
if (object != null && !_.isObjectLike(object)) { | ||||||
if (key === '**' && !rest.length) { | ||||||
// globstar leaves are always selected | ||||||
return [reconstructFieldPath(currPath)]; | ||||||
if (key === '**') { | ||||||
if (!rest.length) { | ||||||
// globstar leaves are always selected | ||||||
return [reconstructFieldPath(currPath)]; | ||||||
} | ||||||
return []; | ||||||
} | ||||||
|
||||||
// there still are paths to traverse, but value is a primitive, stop | ||||||
return []; | ||||||
if (key === '*') { | ||||||
// wildcard position does not exist | ||||||
return []; | ||||||
} | ||||||
// value is a primitive, there are still still paths to traverse that will be likely undefined, we return the entire path | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun fact: you could theoretically go forever now (not for any good reason though), e.g.
Suggested change
|
||||||
return [reconstructFieldPath([...currPath, ...segments])]; | ||||||
} | ||||||
|
||||||
// Use a non-null value so that inexistent fields are still selected | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have another place to store these tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tested aspects are the field selection +
exists
functionality.I don't feel like they are needed though, everything is covered in some way already.