unjs/ufo

Empty array parameters generates invalid URL

Closed this issue · 1 comments

Environment

node: v18.13.0
ufo: version 1.1.2

It might not depends on node version.

Reproduction

> const { stringifyQuery } = require('ufo')
> stringifyQuery({ 'a': 'X', 'b[]': [], c: "Y" })
'a=X&&c=Y'

Describe the bug

If the parameter has an empty array as value, a double ampersand is generated and generated URL is invalid.

Expected behavior:

> stringifyQuery({ 'a': 'X', 'b[]': [], c: "Y" })
'a=X&c=Y'

Especially, the "double ampersand" breaks my Nuxt/Rails hybrid application.

The behavior generates url like the following:

"https://localhost:3001/api/v1/users/search?page=1&perPage=25&sortBy=email-asc&&fullName=aaa;bbbb"

Ruby on Rails parses the query parameter as the following

{"page"=>"1",
 "per_page"=>"25",
 "sort_by"=>"email-asc",
 "full_name"=>"aaa",
 "bbbb"=>nil,
 "format"=>:json,
 "controller"=>"api/v1/users",
 "action"=>"search",
 "user"=>{}}

full_name=aaa;bbbb was separated to "full_name" => "aaa" and "bbbb" => '' and the value after semicolon is ignored in my search form.

Additional context

No response

Logs

No response

pi0 commented

Thanks for reporting issue. It will be fixed in next release.