nextlove icon indicating copy to clipboard operation
nextlove copied to clipboard

Support all array query serializations

Open itelo opened this issue 2 years ago • 2 comments

          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 https://github.com/seamapi/public-tracker/issues/8#issuecomment-1623726030

itelo avatar Jul 06 '23 16:07 itelo

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: https://github.com/seamapi/nextlove/blob/59eeadf5e208348bad147f04d434581573b2c14f/packages/nextlove/src/with-route-spec/index.ts#L49

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 avatar Oct 30 '23 22:10 razor-x

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 [].

razor-x avatar Oct 31 '23 01:10 razor-x