Skip to content

Commit

Permalink
fix: deep clone query when creating routes
Browse files Browse the repository at this point in the history
fix #1690
  • Loading branch information
yyx990803 committed Oct 11, 2017
1 parent 3cc08fb commit effb114
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/util/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export function resolveQuery (
parsedQuery = {}
}
for (const key in extraQuery) {
const val = extraQuery[key]
parsedQuery[key] = Array.isArray(val) ? val.slice() : val
parsedQuery[key] = extraQuery[key]
}
return parsedQuery
}
Expand Down
22 changes: 21 additions & 1 deletion src/util/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ export function createRoute (
router?: VueRouter
): Route {
const stringifyQuery = router && router.options.stringifyQuery

let query: any = location.query || {}
try {
query = clone(query)
} catch (e) {}

const route: Route = {
name: location.name || (record && record.name),
meta: (record && record.meta) || {},
path: location.path || '/',
hash: location.hash || '',
query: location.query || {},
query,
params: location.params || {},
fullPath: getFullPath(location, stringifyQuery),
matched: record ? formatMatch(record) : []
Expand All @@ -28,6 +34,20 @@ export function createRoute (
return Object.freeze(route)
}

function clone (value) {
if (Array.isArray(value)) {
return value.map(clone)
} else if (value && typeof value === 'object') {
const res = {}
for (const key in value) {
res[key] = clone(value[key])
}
return res
} else {
return value
}
}

// the starting route that represents the initial state
export const START = createRoute(null, {
path: '/'
Expand Down
9 changes: 0 additions & 9 deletions test/unit/specs/query.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ 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

0 comments on commit effb114

Please sign in to comment.