Skip to content

fix(query): exclude null-ish values from query encoding #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025

Conversation

ShuviSchwarze
Copy link
Contributor

@ShuviSchwarze ShuviSchwarze commented Feb 27, 2025

Closes: elysiajs/elysia#1094
Relevant: #129 #127

This PR explicitly exclude undefined and null properties from query string encoding due to URI query lack of support for sending null-ish values.

This lead to unintuitive behaviour on elysia, as well as t.Optional not working for the bellow case

const query = {
	username: "testUser",
	alias: undefined
}

const { data } = await client.query.get({
	query
})
// ?username=testUser&alias=undefined
// { username: "testUser", alias: "undefined" } 

From elysiajs/elysia#1094 (comment), this proposes null-ish values be excluded from query params following this recommendation

New behaviour

const query = { username: undefined }

const { data } = await client.query.get({
	query: { username: undefined }
})
// ?username=testUser
// { username: "testUser", alias: undefined } 

Comment on lines +75 to +81
const nonNullishQuery = query
? Object.fromEntries(
Object.entries(query).filter(
([_, val]) => val !== undefined && val !== null
)
)
: null
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const nonNullishQuery = query
? Object.fromEntries(
Object.entries(query).filter(
([_, val]) => val !== undefined && val !== null
)
)
: null
const nonNullishQuery = query
? Object.fromEntries(
Object.entries(query).filter(
([_, val]) => val != null)
)
)
: null

js quirk: != null catches both undefined and null

Copy link
Contributor Author

@ShuviSchwarze ShuviSchwarze Feb 27, 2025

Choose a reason for hiding this comment

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

Unless for performance reasons, I dont think relying on implicit behaviours is good

@SaltyAom SaltyAom merged commit 84679e9 into elysiajs:main May 5, 2025
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.

Eden converts undefined to "undefined" in query
3 participants