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

make a copy when query param value is an array (fix #1182) #1291

Merged
merged 2 commits into from
Apr 6, 2017
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
25 changes: 11 additions & 14 deletions src/util/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ export function resolveQuery (
query: ?string,
extraQuery: Dictionary<string> = {}
): Dictionary<string> {
if (query) {
let parsedQuery
try {
parsedQuery = parseQuery(query)
} catch (e) {
process.env.NODE_ENV !== 'production' && warn(false, e.message)
parsedQuery = {}
}
for (const key in extraQuery) {
parsedQuery[key] = extraQuery[key]
}
return parsedQuery
} else {
return extraQuery
Copy link
Member

Choose a reason for hiding this comment

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

It worries me that deleting this part didn't break the test. @fnlctrl Do you have an idea about this?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is already an empty value check in parseQuery function.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but in the other case we were skipping the whole parseQuery and for loop and returning right away the extraQuery instead

let parsedQuery
try {
parsedQuery = parseQuery(query || '')
} catch (e) {
process.env.NODE_ENV !== 'production' && warn(false, e.message)
parsedQuery = {}
}
for (const key in extraQuery) {
const val = extraQuery[key]
parsedQuery[key] = Array.isArray(val) ? val.slice() : val
}
return parsedQuery
}

function parseQuery (query: string): Dictionary<string> {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/specs/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ describe('Query utils', () => {
baz: 'qux'
}))
})

it('should make a copy when param value is an array', () => {
const arr = ['bar']
const query = resolveQuery('', { foo: arr })
arr.push('baz')
expect(JSON.stringify(query)).toBe(JSON.stringify({
foo: ['bar']
}))
})
})

describe('stringifyQuery', () => {
Expand Down