Skip to content
Myr

fix(request): return empty string for empty catch-all param (#4395)

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

* test: add catch-all route test for empty segment handling fix: ensure undefined parameters are handled correctly in HonoRequest * ci: apply automated fixes * fix(request): handle empty string parameters in req.param() and decoding logic * test: add catch-all route test for empty segment handling fix: ensure undefined parameters are handled correctly in HonoRequest * ci: apply automated fixes * fix: return empty string for missing params in req.param() method * fix(param): simplify HonoRequest instantiation for zero-length match test * test: add catch-all route test for empty segment handling fix: ensure undefined parameters are handled correctly in HonoRequest * ci: apply automated fixes * correct the logic and the tests Co-authored-by: Taku Amano <taku@taaas.jp> * remove unnecessary logics ---------

authored by Amit Singh · committed by GitHub · parent 9987081 · Browse files · 3 files+49 added−2 removed
This commit

State

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

Trailers

  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

3 files+49 added−2 removed

  • tests:+47 added−0 removed
  • other:+2 added−2 removed

3 files+49 added−2 removed

modified:src/hono.test.ts+14 added
3656 unmodified lines
3657 expect(res.status).toBe(500)3658 expect(await res.text()).toBe('Custom error "API Test error"')3659 })3660})36613662describe('Catch-all route with empty segment', () => {3663 it('Should return empty string for empty catch-all param', async () => {3664 const app = new Hono()3665 app.get('/:remaining{.*}', (c) => {3666 const remaining = c.req.param('remaining')3667 return c.json({ type: typeof remaining, value: remaining })3668 })3669 const res = await app.request('http://localhost/')3670 expect(res.status).toBe(200)3671 const json = await res.json()3672 expect(json).toEqual({ type: 'string', value: '' })3673 })3674})
modified:src/request.test.ts+33 added
36 unmodified lines
37 })38})3940describe('Param', () => {41 test('req.param() should return empty string for zero-length match', () => {42 // Simulate a route like '/:remaining{.*}' matching '/'43 const rawRequest = new Request('http://localhost/')44 const req = new HonoRequest<'/:remaining{.*}'>(rawRequest, '/', [45 [[[undefined, {} as RouterRoute], { remaining: 0 }]],46 [''], // ParamStash with empty string for remaining47 ])4849 // Single param access should be empty string, not undefined50 expect(req.param('remaining')).toBe('')5152 // All params should include key with empty string value53 const all = req.param()54 expect(all).toEqual({ remaining: '' })55 })56 test('req.param() with ParamStash', () => {57 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')58 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [59 [
26 unmodified lines
86 req.routeIndex = 187 expect(req.param('id')).toBe('456')88 expect(req.param('name')).toBe('key')89 })9091 test('req.param() returns empty string for missing param', () => {92 const rawRequest = new Request('http://localhost')93 const req = new HonoRequest<'/:remaining'>(rawRequest, '/', [94 [[[undefined, {} as RouterRoute], { remaining: 0 }]],95 [''],96 ])97 expect(req.param('remaining')).toBe('')98 })99100 test('req.param() without argument returns object with empty string for missing param', () => {101 const rawRequest = new Request('http://localhost')102 const req = new HonoRequest<'/:remaining'>(rawRequest, '/', [103 [[[undefined, {} as RouterRoute], { remaining: 0 }]],104 [''],105 ])106 expect(req.param()).toEqual({ remaining: '' })107 })108})109110describe('matchedRoutes', () => {111 test('req.routePath', () => {
269 unmodified lines
modified:src/request.ts+2 added-2 removed
96 unmodified lines
9798 #getDecodedParam(key: string): string | undefined {99 const paramKey = this.#matchResult[0][this.routeIndex][1][key]100 const param = this.#getParamValue(paramKey)101 return param ? (/\%/.test(param) ? tryDecodeURIComponent(param) : param) : undefined101 return param && /\%/.test(param) ? tryDecodeURIComponent(param) : param102 }103104 #getAllDecodedParams(): Record<string, string> {105 const decoded: Record<string, string> = {}106107 const keys = Object.keys(this.#matchResult[0][this.routeIndex][1])108 for (const key of keys) {109 const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key])110 if (value && typeof value === 'string') {110 if (value !== undefined) {111 decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value112 }113 }114
305 unmodified lines