fix(trie-router): fixed same name path params matter (#1992)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(trie-router): fixed same name path params matter * denoify
authored by Yusuke Wada · committed by GitHub · parent 7d8cdd5 · Browse files · 3 files·+54 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+54 added−33 removed
- tests:+24 added−9 removed
- other:+30 added−24 removed
Provenance
- Commit
f380467- Tree
9c68a61- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
7d8cdd5
3 files changed+54 added−33 removed
modified:deno_dist/router/trie-router/node.ts+15 added-12 removed
75 unmodified lines76 const m: Record<string, HandlerSet<T>> = {}77 78 const handlerSet: HandlerSet<T> = {79 handler,80- possibleKeys,80+ possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),81 name: this.name,82 score: this.order,83 }84 6 unmodified lines91 // getHandlerSets92 private gHSets(93 node: Node<T>,94 method: string,95+ nodeParams: Record<string, string>,96 params: Record<string, string>97 ): HandlerParamsSet<T>[] {98 const handlerSets: HandlerParamsSet<T>[] = []99 for (let i = 0, len = node.methods.length; i < len; i++) {100 const m = node.methods[i]101 const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>102+ const processedSet: Record<string, boolean> = {}103 if (handlerSet !== undefined) {104 handlerSet.params = {}103- handlerSet.possibleKeys.map((key) => {104- handlerSet.params[key] = params[key]105+ handlerSet.possibleKeys.forEach((key) => {106+ const processed = processedSet[handlerSet.name]107+ handlerSet.params[key] =108+ params[key] && !processed ? params[key] : nodeParams[key] ?? params[key]109+ processedSet[handlerSet.name] = true110 })111 handlerSets.push(handlerSet)112 }113 }2 unmodified lines116 117 search(method: string, path: string): [[T, Params][]] {118 const handlerSets: HandlerParamsSet<T>[] = []119 this.params = {}120+ const params: Record<string, string> = {}121 122 // eslint-disable-next-line @typescript-eslint/no-this-alias123 const curNode: Node<T> = this124 let curNodes = [curNode]12 unmodified lines137 nextNode.params = node.params138 if (isLast === true) {139 // '/hello/*' => match '/hello'140 if (nextNode.children['*']) {135- handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params))141+ handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params, {}))142 }137- handlerSets.push(...this.gHSets(nextNode, method, node.params))143+ handlerSets.push(...this.gHSets(nextNode, method, node.params, {}))144 } else {145 tempNodes.push(nextNode)146 }147 }148 149 for (let k = 0, len3 = node.patterns.length; k < len3; k++) {144- const params: Record<string, string> = {}150 const pattern = node.patterns[k]151 152 // Wildcard153 // '/hello/*/foo' => match /hello/bar/foo154 if (pattern === '*') {155 const astNode = node.children['*']156 if (astNode) {152- handlerSets.push(...this.gHSets(astNode, method, node.params))157+ handlerSets.push(...this.gHSets(astNode, method, node.params, {}))158 tempNodes.push(astNode)159 }160 continue161 }7 unmodified lines169 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js170 const restPathString = parts.slice(i).join('/')171 if (matcher instanceof RegExp && matcher.test(restPathString)) {172 params[name] = restPathString168- handlerSets.push(...this.gHSets(child, method, { ...params, ...node.params }))173+ handlerSets.push(...this.gHSets(child, method, node.params, params))174 continue175 }176 177 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {178 if (typeof key === 'string') {179 params[name] = part180 if (isLast === true) {176- handlerSets.push(...this.gHSets(child, method, { ...params, ...node.params }))181+ handlerSets.push(...this.gHSets(child, method, params, node.params))182 if (child.children['*']) {178- handlerSets.push(179- ...this.gHSets(child.children['*'], method, { ...params, ...node.params })180- )183+ handlerSets.push(...this.gHSets(child.children['*'], method, node.params, params))184 }185 } else {186 child.params = { ...params }187 tempNodes.push(child)15 unmodified linesmodified:src/router/trie-router/node.test.ts+24 added-9 removed
741 unmodified lines742 })743 })744 745 describe('The same name is used for path params', () => {746+ describe('Basic', () => {746- const node = new Node()747- node.insert('get', '/:a', 'a')748- node.insert('get', '/:b/:a', 'ba')749- it('/about/me', () => {750- const [res] = node.search('get', '/about/me')751- expect(res).not.toBeNull()752- expect(res.length).toBe(1)753- expect(res[0][0]).toEqual('ba')754- expect(res[0][1]).toEqual({ b: 'about', a: 'me' })747+ const node = new Node()748+ node.insert('get', '/:a/:b/:c', 'abc')749+ node.insert('get', '/:a/:b/:c/:d', 'abcd')750+ it('/1/2/3', () => {751+ const [res] = node.search('get', '/1/2/3')752+ expect(res).not.toBeNull()753+ expect(res.length).toBe(1)754+ expect(res[0][0]).toEqual('abc')755+ expect(res[0][1]).toEqual({ a: '1', b: '2', c: '3' })756+ })757 })758+759+ describe('Complex', () => {760+ const node = new Node()761+ node.insert('get', '/:a', 'a')762+ node.insert('get', '/:b/:a', 'ba')763+ it('/about/me', () => {764+ const [res] = node.search('get', '/about/me')765+ expect(res).not.toBeNull()766+ expect(res.length).toBe(1)767+ expect(res[0][0]).toEqual('ba')768+ expect(res[0][1]).toEqual({ b: 'about', a: 'me' })769+ })770+ })771 })modified:src/router/trie-router/node.ts+15 added-12 removed
75 unmodified lines76 const m: Record<string, HandlerSet<T>> = {}77 78 const handlerSet: HandlerSet<T> = {79 handler,80- possibleKeys,80+ possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),81 name: this.name,82 score: this.order,83 }84 6 unmodified lines91 // getHandlerSets92 private gHSets(93 node: Node<T>,94 method: string,95+ nodeParams: Record<string, string>,96 params: Record<string, string>97 ): HandlerParamsSet<T>[] {98 const handlerSets: HandlerParamsSet<T>[] = []99 for (let i = 0, len = node.methods.length; i < len; i++) {100 const m = node.methods[i]101 const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>102+ const processedSet: Record<string, boolean> = {}103 if (handlerSet !== undefined) {104 handlerSet.params = {}103- handlerSet.possibleKeys.map((key) => {104- handlerSet.params[key] = params[key]105+ handlerSet.possibleKeys.forEach((key) => {106+ const processed = processedSet[handlerSet.name]107+ handlerSet.params[key] =108+ params[key] && !processed ? params[key] : nodeParams[key] ?? params[key]109+ processedSet[handlerSet.name] = true110 })111 handlerSets.push(handlerSet)112 }113 }2 unmodified lines116 117 search(method: string, path: string): [[T, Params][]] {118 const handlerSets: HandlerParamsSet<T>[] = []119 this.params = {}120+ const params: Record<string, string> = {}121 122 // eslint-disable-next-line @typescript-eslint/no-this-alias123 const curNode: Node<T> = this124 let curNodes = [curNode]12 unmodified lines137 nextNode.params = node.params138 if (isLast === true) {139 // '/hello/*' => match '/hello'140 if (nextNode.children['*']) {135- handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params))141+ handlerSets.push(...this.gHSets(nextNode.children['*'], method, node.params, {}))142 }137- handlerSets.push(...this.gHSets(nextNode, method, node.params))143+ handlerSets.push(...this.gHSets(nextNode, method, node.params, {}))144 } else {145 tempNodes.push(nextNode)146 }147 }148 149 for (let k = 0, len3 = node.patterns.length; k < len3; k++) {144- const params: Record<string, string> = {}150 const pattern = node.patterns[k]151 152 // Wildcard153 // '/hello/*/foo' => match /hello/bar/foo154 if (pattern === '*') {155 const astNode = node.children['*']156 if (astNode) {152- handlerSets.push(...this.gHSets(astNode, method, node.params))157+ handlerSets.push(...this.gHSets(astNode, method, node.params, {}))158 tempNodes.push(astNode)159 }160 continue161 }7 unmodified lines169 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js170 const restPathString = parts.slice(i).join('/')171 if (matcher instanceof RegExp && matcher.test(restPathString)) {172 params[name] = restPathString168- handlerSets.push(...this.gHSets(child, method, { ...params, ...node.params }))173+ handlerSets.push(...this.gHSets(child, method, node.params, params))174 continue175 }176 177 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {178 if (typeof key === 'string') {179 params[name] = part180 if (isLast === true) {176- handlerSets.push(...this.gHSets(child, method, { ...params, ...node.params }))181+ handlerSets.push(...this.gHSets(child, method, params, node.params))182 if (child.children['*']) {178- handlerSets.push(179- ...this.gHSets(child.children['*'], method, { ...params, ...node.params })180- )183+ handlerSets.push(...this.gHSets(child.children['*'], method, node.params, params))184 }185 } else {186 child.params = { ...params }187 tempNodes.push(child)15 unmodified lines