Skip to content
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

Support all array query serializations #77

Open
itelo opened this issue Jul 6, 2023 · 2 comments
Open

Support all array query serializations #77

itelo opened this issue Jul 6, 2023 · 2 comments

Comments

@itelo
Copy link
Contributor

itelo commented Jul 6, 2023

          We should look into how Nextlove is validating arrays. There seems to be a validation process in place that works well for `POST`, but not for `GET`.

Originally posted by @itelo in seamapi/public-tracker#8 (comment)

@razor-x razor-x changed the title How is Nextlove validating arrays? There seems to be a validation process in place that works well for POST, but not for GET. Support all array query serializations Oct 30, 2023
@razor-x
Copy link
Contributor

razor-x commented Oct 30, 2023

We need to support a the "common" array serialization formats.

Current support as of this comment

export type QueryArrayFormat = "brackets" | "comma" | "repeat"

Defaults must be updated when adding a new one:

export const DEFAULT_ARRAY_FORMATS: QueryArrayFormats = [

I think the main one we are missing is indexed which is like brackets but includes the index like ?foo[0]=a&foo[1]=b. This is the default used by axios (and possibly Ruby's HTTP gem and others).

Note that the "standard" format is generally agreed to be repeat since this is what URLSearchParams does (and does not mangle the name or value in the query string), but all formats are "non-standard".

All formats have drawbacks, but client libs are recommended to implement repeat. Critically, repeat has an ambiguity since a single element array containing the empty string foo= could be interpreted as either foo=[''] or foo=[]. This library will interpret this as [] which is often the intended behavior and more useful than ['']. Client libraries should disallow passing [''] to array params to prevent silently changing the intention of the caller.

A reference implementation for a compatible serializer may be found at https://github.com/seamapi/javascript-http/blob/main/src/lib/params-serializer.ts

@razor-x
Copy link
Contributor

razor-x commented Oct 31, 2023

This library will interpret this as []

There may be a bug where this lib is actually parsing foo= as [undefined] or ['']. We need some tests around these edge cases. It should parse foo= as [].

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

No branches or pull requests

2 participants