Skip to content
Myr

fix(trie-router): bug for routing orders with named param (#704)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 08a1b78 · Browse files · 4 files+49 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+49 added−22 removed

  • tests:+27 added−14 removed
  • other:+22 added−8 removed

4 files+49 added−22 removed

modified:deno_dist/router/trie-router/node.ts+11 added-4 removed
31 unmodified lines
32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36 shouldCapture: boolean = false3738 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {39 this.children = children || {}40 this.methods = []
15 unmodified lines
56 let curNode: Node<T> = this57 const parts = splitPath(path)5859 const parentPatterns: Pattern[] = []5960 const errorMessage = (name: string): string => {61 return `Duplicate param name, use another name instead of '${name}' - ${method} ${path} <--- '${name}'`62 }63
10 unmodified lines
7475 const pattern = getPattern(p)76 if (pattern) {77 if (typeof pattern === 'object') {78 this.shouldCapture = true79 for (let j = 0, len = parentPatterns.length; j < len; j++) {80 if (typeof parentPatterns[j] === 'object' && parentPatterns[j][1] === pattern[1]) {81 throw new Error(errorMessage(pattern[1]))82 }
4 unmodified lines
87 }88 curNode.patterns.push(pattern)89 parentPatterns.push(...curNode.patterns)90 }9091 parentPatterns.push(...curNode.patterns)92 curNode = curNode.children[p]93 curNode.shouldCapture = this.shouldCapture94 }9596 if (!curNode.methods.length) {97 curNode.methods = []
82 unmodified lines
180 tempNodes.push(node.children[key])181 }182 }183183 // '/book/a' => not-slug184 // '/book/:slug' => slug184 // `/book/a` => no-slug185 // `/book/:slug` => slug186 // `/book/b` => no-slug-b187 // GET /book/a ~> no-slug, param['slug'] => undefined188 // GET /book/foo ~> slug, param['slug'] => foo189 // GET /book/b ~> no-slug-b, param['slug'] => b190 if (typeof name === 'string' && !matched) {191 params[name] = part192 } else {193 if (node.children[part] && node.children[part].shouldCapture) {194 params[name] = part195 }196 }197 }198 }199 }
19 unmodified lines
modified:src/router/trie-router/node.test.ts+7 added
552 unmodified lines
553describe('Routing order With named parameters', () => {554 const node = new Node()555 node.insert('get', '/book/a', 'no-slug')556 node.insert('get', '/book/:slug', 'slug')557 node.insert('get', '/book/b', 'no-slug-b')558 it('/book/a', () => {559 const res = node.search('get', '/book/a')560 expect(res).not.toBeNull()561 expect(res?.handlers).toEqual(['no-slug', 'slug'])
4 unmodified lines
566 expect(res).not.toBeNull()567 expect(res?.handlers).toEqual(['slug'])568 expect(res?.params['slug']).toBe('foo')569 })570 it('/book/b', () => {571 const res = node.search('get', '/book/b')572 expect(res).not.toBeNull()573 expect(res?.handlers).toEqual(['slug', 'no-slug-b'])574 expect(res?.params['slug']).toBe('b')575 })576})
modified:src/router/trie-router/node.ts+11 added-4 removed
31 unmodified lines
32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36 shouldCapture: boolean = false3738 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {39 this.children = children || {}40 this.methods = []
15 unmodified lines
56 let curNode: Node<T> = this57 const parts = splitPath(path)5859 const parentPatterns: Pattern[] = []5960 const errorMessage = (name: string): string => {61 return `Duplicate param name, use another name instead of '${name}' - ${method} ${path} <--- '${name}'`62 }63
10 unmodified lines
7475 const pattern = getPattern(p)76 if (pattern) {77 if (typeof pattern === 'object') {78 this.shouldCapture = true79 for (let j = 0, len = parentPatterns.length; j < len; j++) {80 if (typeof parentPatterns[j] === 'object' && parentPatterns[j][1] === pattern[1]) {81 throw new Error(errorMessage(pattern[1]))82 }
4 unmodified lines
87 }88 curNode.patterns.push(pattern)89 parentPatterns.push(...curNode.patterns)90 }9091 parentPatterns.push(...curNode.patterns)92 curNode = curNode.children[p]93 curNode.shouldCapture = this.shouldCapture94 }9596 if (!curNode.methods.length) {97 curNode.methods = []
82 unmodified lines
180 tempNodes.push(node.children[key])181 }182 }183183 // '/book/a' => not-slug184 // '/book/:slug' => slug184 // `/book/a` => no-slug185 // `/book/:slug` => slug186 // `/book/b` => no-slug-b187 // GET /book/a ~> no-slug, param['slug'] => undefined188 // GET /book/foo ~> slug, param['slug'] => foo189 // GET /book/b ~> no-slug-b, param['slug'] => b190 if (typeof name === 'string' && !matched) {191 params[name] = part192 } else {193 if (node.children[part] && node.children[part].shouldCapture) {194 params[name] = part195 }196 }197 }198 }199 }
19 unmodified lines
modified:src/router/trie-router/router.test.ts+20 added-14 removed