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
Provenance
- Commit
cf73de1- Tree
78b6712- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
08a1b78
4 files changed+49 added−22 removed
modified:deno_dist/router/trie-router/node.ts+11 added-4 removed
31 unmodified lines32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36+ shouldCapture: boolean = false37 38 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {39 this.children = children || {}40 this.methods = []15 unmodified lines56 let curNode: Node<T> = this57 const parts = splitPath(path)58 59 const parentPatterns: Pattern[] = []59-60 const errorMessage = (name: string): string => {61 return `Duplicate param name, use another name instead of '${name}' - ${method} ${path} <--- '${name}'`62 }63 10 unmodified lines74 75 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 lines87 }88 curNode.patterns.push(pattern)89 parentPatterns.push(...curNode.patterns)90 }90-91 parentPatterns.push(...curNode.patterns)92 curNode = curNode.children[p]93+ curNode.shouldCapture = this.shouldCapture94 }95 96 if (!curNode.methods.length) {97 curNode.methods = []82 unmodified lines180 tempNodes.push(node.children[key])181 }182 }183 183- // '/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 linesmodified:src/router/trie-router/node.test.ts+7 added
552 unmodified lines553 describe('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 lines566 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 lines32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36+ shouldCapture: boolean = false37 38 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {39 this.children = children || {}40 this.methods = []15 unmodified lines56 let curNode: Node<T> = this57 const parts = splitPath(path)58 59 const parentPatterns: Pattern[] = []59-60 const errorMessage = (name: string): string => {61 return `Duplicate param name, use another name instead of '${name}' - ${method} ${path} <--- '${name}'`62 }63 10 unmodified lines74 75 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 lines87 }88 curNode.patterns.push(pattern)89 parentPatterns.push(...curNode.patterns)90 }90-91 parentPatterns.push(...curNode.patterns)92 curNode = curNode.children[p]93+ curNode.shouldCapture = this.shouldCapture94 }95 96 if (!curNode.methods.length) {97 curNode.methods = []82 unmodified lines180 tempNodes.push(node.children[key])181 }182 }183 183- // '/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 linesmodified:src/router/trie-router/router.test.ts+20 added-14 removed
133 unmodified linesExpand all1341351361371381391401411421431441451461471481491501511521531541551561571581591601612 unmodified lines164165166167152153154155156157158159160168169170171172162163164165166173133 unmodified linesExpand all expect(res?.handlers).toEqual(['page', 'fallback']) })})
describe('Optional route', () => { const router = new TrieRouter<string>() router.add('GET', '/api/animals/:type?', 'animals') it('GET /api/animals/dog', async () => { const res = router.match('GET', '/api/animals/dog') expect(res?.handlers).toEqual(['animals']) expect(res?.params['type']).toBe('dog') }) it('GET /api/animals', async () => { const res = router.match('GET', '/api/animals') expect(res?.handlers).toEqual(['animals']) expect(res?.params['type']).toBeUndefined() })})
describe('routing order with named parameters', () => { const router = new TrieRouter<string>() router.add('GET', '/book/a', 'no-slug') router.add('GET', '/book/:slug', 'slug') router.add('GET', '/book/b', 'no-slug-b') it('GET /book/a', async () => { const res = router.match('GET', '/book/a') expect(res?.handlers).toEqual(['no-slug', 'slug']) expect(res?.params['slug']).toBeUndefined()2 unmodified lines const res = router.match('GET', '/book/foo') expect(res?.handlers).toEqual(['slug']) expect(res?.params['slug']).toBe('foo') })})
describe('Optional route', () => { const router = new TrieRouter<string>() router.add('GET', '/api/animals/:type?', 'animals') it('GET /api/animals/dog', async () => { const res = router.match('GET', '/api/animals/dog') expect(res?.handlers).toEqual(['animals']) expect(res?.params['type']).toBe('dog') it('GET /book/b', async () => { const res = router.match('GET', '/book/b') expect(res?.handlers).toEqual(['slug', 'no-slug-b']) expect(res?.params['slug']).toBe('b') }) it('GET /api/animals', async () => { const res = router.match('GET', '/api/animals') expect(res?.handlers).toEqual(['animals']) expect(res?.params['type']).toBeUndefined() })})