Skip to content

fix: avoid trailing ? when queryString option resolves to empty string#459

Merged
fox1t merged 1 commit intofastify:mainfrom
fooddilsn:fix/querystring-empty-guard
Mar 5, 2026
Merged

fix: avoid trailing ? when queryString option resolves to empty string#459
fox1t merged 1 commit intofastify:mainfrom
fooddilsn:fix/querystring-empty-guard

Conversation

@fooddilsn
Copy link
Copy Markdown
Contributor

Summary

When the queryString option resolves to an empty string, getQueryString unconditionally prepends ?, resulting in a trailing ? on the upstream request path (e.g. /path? instead of /path).

This affects both forms:

  • Function form: '?' + opts.queryString(...) when the function returns ''
  • Object form: '?' + querystring.stringify(opts.queryString) when the object is empty {}

This is inconsistent with the default code path (no queryString option), which correctly returns '' when there's no query string.

Fix

Guard both paths to only prepend ? when the result is non-empty:

  const qs = opts.queryString(search, reqUrl, request)
  return qs ? '?' + qs : ''

Closes #458

Copy link
Copy Markdown
Member

@fox1t fox1t left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

Lgtm

@fox1t fox1t merged commit 4b87813 into fastify:main Mar 5, 2026
14 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.

queryString option always prepends "?" even when result is empty

3 participants