Skip to content

Conversation

amitksingh0880
Copy link
Contributor

Fix: Ensure c.req.param() returns "" (empty string) for empty path parameters

Previously, when a route parameter matched an empty segment (e.g., / for /:remaining{.*}), c.req.param() returned undefined, which did not match its type signature (string).

This change updates the parameter extraction logic to return an empty string ("") instead of undefined for empty matches, ensuring runtime behavior matches the declared types and preventing potential runtime errors.

Also adds a test to verify this behavior.

fix: ensure undefined parameters are handled correctly in HonoRequest
Copy link

codecov bot commented Sep 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.25%. Comparing base (0af31fa) to head (cd86b86).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4395   +/-   ##
=======================================
  Coverage   91.25%   91.25%           
=======================================
  Files         171      171           
  Lines       10913    10913           
  Branches     3143     3144    +1     
=======================================
  Hits         9959     9959           
  Misses        953      953           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@usualoma
Copy link
Member

usualoma commented Sep 3, 2025

Hi @amitksingh0880, thank you for creating the pull request! I believe the changes you made resolve the issue, but I have a few requests.

  • For c.req.param() without arguments, I think it would be more consistent if it returned an empty string when no value is present
  • Please add tests to “src/request.test.ts”
  • (This is incidental, so either way is fine) I think #getDecodedParam could be written a bit more concisely

I think it would be good to make it like #4396. Could you please consider this?

@amitksingh0880
Copy link
Contributor Author

Hi @usualoma, thank you for the detailed feedback! I’ll look into the points you mentioned.

  • Updating the behavior of c.req.param()
  • Adding the requested tests, and reviewing #getDecodedParam for conciseness.

I’ll also make sure it aligns with #4396. Will update the PR accordingly.

@yusukebe
Copy link
Member

@amitksingh0880 Thank you for the PR!

The followings fail:

I think these should not fail. For example, please imagine the app:

app.get('/foo/:foo', (c) => {
  const bar = c.req.param('bar')
  return c.json({ bar })
})

When accessing /foo/abc. The value of bar should be undefined because the key (bar) is not found in params, but in this PR it returns "".

So the test you wrote:

Are wrong. Those should be like the following:

diff --git a/src/request.test.ts b/src/request.test.ts
index e04d4dfc..7a063deb 100644
--- a/src/request.test.ts
+++ b/src/request.test.ts
@@ -64,7 +64,7 @@ describe('Param', () => {
     ])

     expect(req.param('id')).toBe('123')
-    expect(req.param('name')).toBe('')
+    expect(req.param('name')).toBe(undefined)

     req.routeIndex = 1
     expect(req.param('id')).toBe('123')
@@ -81,7 +81,7 @@ describe('Param', () => {
     ])

     expect(req.param('id')).toBe('123')
-    expect(req.param('name')).toBe('')
+    expect(req.param('name')).toBe(undefined)

     req.routeIndex = 1
     expect(req.param('id')).toBe('456')

@usualoma What do you think of this?

@amitksingh0880
Copy link
Contributor Author

Hey! @yusukebe thank you for the comment.

I will look into the described changes and update the same.

@yusukebe yusukebe changed the title test: add catch-all route test for empty segment handling fix: add catch-all route test for empty segment handling Sep 18, 2025
@yusukebe yusukebe changed the title fix: add catch-all route test for empty segment handling fix(hono-request): return empty string for empty catch-all param Sep 18, 2025
@yusukebe yusukebe changed the title fix(hono-request): return empty string for empty catch-all param fix(request): return empty string for empty catch-all param Sep 18, 2025
@yusukebe yusukebe requested a review from usualoma September 18, 2025 08:05
@yusukebe
Copy link
Member

I've updated the logic and tests myself. This may be good.

@usualoma Can you review this?

Copy link
Member

@usualoma usualoma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@yusukebe
Copy link
Member

@amitksingh0880 @usualoma I'll merge this now. Thank you!

@yusukebe yusukebe merged commit 3fe60f1 into honojs:main Sep 18, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants