Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/canonical-omits-json-object.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thomasfosterau/effect-jsonapi": patch
---

`Query.canonical` / `Query.canonicalPairs` (and so the `self` and pagination links built from them) omit a flat query member whose encoded value is a JSON object, as they already omit `null` (#536). A JSON object has no flat wire form; before, it was written as `key=[object Object]`.
13 changes: 13 additions & 0 deletions src/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,19 @@ describe("Query.canonical", () => {
);
});

it("omits a JSON object member, which has no flat wire form (#536)", () => {
const wire = Schema.Struct({
location: Schema.optionalKey(Schema.Struct({ x: Schema.String })),
flag: Schema.optionalKey(Schema.Boolean),
...Query.Page.Offset,
}).pipe(Schema.encodeKeys({ limit: "page[limit]", offset: "page[offset]" }));
// never `location=[object Object]`
expect(Query.canonicalPairs(wire)({ location: { x: "a" }, flag: false, limit: 5 })).toEqual([
["page[limit]", "5"],
["flag", "false"],
]);
});

it("serialise: RFC 3986 percent-encoding, brackets readable in keys, spaces %20, commas %2C", () => {
expect(
Query.serialise([
Expand Down
4 changes: 2 additions & 2 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2018,8 +2018,8 @@ export const serialise: (pairs: Iterable<Pair>) => string = serialisePairs;
* builders emit, so a `self` link and the canonical string agree
* 6. any other key (a consumer's own flat schema), sorted by key
*
* An absent (`undefined` or `null`) value is omitted, an empty string is
* kept (`key=`), and an array is the repeated key, one pair per item in
* An absent (`undefined` or `null`) value is omitted, as is a JSON object
* (it has no flat wire form), an empty string is kept (`key=`), and an array is the repeated key, one pair per item in
* order (`include` excepted: its comma form) — see {@link FlatQuerySchema}.
*
* @example
Expand Down
9 changes: 6 additions & 3 deletions src/internal/canonical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,19 @@ export type FlatQuery = Readonly<Partial<Record<string, Schema.Json>>>;
// per item — the repeated-key spelling `UrlParams.toRecord` decodes back to
// an array — except `include`, whose comma form is the schema's own encoding
// of the set (the query schema already joins it; a consumer's flat schema
// may not have). An absent member and `null` have no wire form and are omitted.
// may not have). An absent member, `null` and a JSON object have no flat wire
// form and are omitted (#536).
const pairsOf = (key: string, member: FlatValue): ReadonlyArray<Pair> => {
if (Option.isNone(member) || member.value === null) return [];
const value = member.value;
if (typeof value === "string") return [[key, value]];
if (typeof value === "number" || typeof value === "boolean") return [[key, String(value)]];
if (Array.isArray(value)) {
return key === "include"
? [[key, value.map(String).join(",")]]
: value.map((item) => [key, String(item)]);
}
return [[key, String(value)]];
return [];
};

/**
Expand All @@ -95,7 +97,8 @@ const pairsOf = (key: string, member: FlatValue): ReadonlyArray<Pair> => {
* link builders emit), keys the schema does not declare after them, sorted
* 6. everything else (a consumer's own flat keys), sorted by key
*
* `undefined` and `null` values are omitted; an empty string is kept
* `undefined`, `null` and JSON object values are omitted (an object has no
* flat wire form); an empty string is kept
* (`key=`); an array is the repeated key, one pair per item in order
* (`include` excepted: the comma form). Sorting is stable, so a repeated
* key's items keep their order.
Expand Down
4 changes: 0 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ export default defineConfig({
"typescript/no-unnecessary-type-parameters": "warn",
// 43 sites, 39 in src/ (most in Resource.ts's mapped types).
"typescript/no-generated-empty-object-type": "warn",
// 1 site: `pairsOf` in src/internal/canonical.ts stringifies a JSON
// object member as `[object Object]`. A JSON object has no flat wire
// form; picking one (omit it, or fail) is a behaviour change (#536).
"typescript/no-base-to-string": "warn",
// `@effect/tsgo`'s preset sets its rules at `warn`; the `correctness` /
// `suspicious` categories above raised these three to `error`. Back at
// the preset's level, counted: 106 sites (94 in tests and examples),
Expand Down
Loading