Skip to content

Commit

Permalink
Fix the uniqueItems implementation to accomodate undefined values n…
Browse files Browse the repository at this point in the history
…ested inside lists of objects.
  • Loading branch information
pose committed Apr 4, 2023
1 parent 9a52172 commit dff4548
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
6 changes: 6 additions & 0 deletions smithy-typescript-ssdk-libs/server-common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# server-common Changelog

## 1.0.0-alpha.10 (2023-04-04)

### Bug Fixes

- Fix `uniqueItems` validation not to throw when undefined values are nested inside a list of objects.

## 1.0.0-alpha.9 (2023-03-16)

### Features
Expand Down
15 changes: 14 additions & 1 deletion smithy-typescript-ssdk-libs/server-common/src/unique.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ describe("findDuplicates", () => {
Uint8Array.of(4, 5, 6),
]);
});
it("nulls", () => {
expect(findDuplicates([null, 1, null])).toEqual([null]);
});
it("undefineds", () => {
const arr: Array<any> = [undefined, 1, undefined];
expect(findDuplicates(arr)).toEqual([undefined]);
});
it("objects", () => {
expect(findDuplicates([{ a: "b" }, { b: [1, 2, 3] }, { a: "b" }, { a: "b" }])).toEqual([{ a: "b" }]);
expect(findDuplicates([{ a: "b" }, { b: 1, c: 2 }, { c: 2, b: 1 }])).toEqual([{ b: 1, c: 2 }]);
Expand Down Expand Up @@ -101,8 +108,14 @@ describe("findDuplicates", () => {
it("blobs", () => {
expect(findDuplicates([Uint8Array.of(1, 2, 3), Uint8Array.of(1, 2)])).toEqual([]);
});
it("nulls", () => {
expect(findDuplicates([null, 1])).toEqual([]);
});
it("undefineds", () => {
const arr: Array<any> = [undefined, 1];
expect(findDuplicates(arr)).toEqual([]);
});
});

// This is relatively slow and may be flaky if the input size is tuned to let it run reasonably fast
it.skip("is faster than the naive implementation", () => {
const input: Input[] = [true, false, 1, 2, 3, 4, 5, 6];
Expand Down
4 changes: 4 additions & 0 deletions smithy-typescript-ssdk-libs/server-common/src/unique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const hash = (input: Input): string => {
* @return a canonical string representation
*/
const canonicalize = (input: Input): string => {
if (input === undefined) {
return "undefined()";
}

if (input === null) {
return "null()";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,13 @@ describe("uniqueItems", () => {
path: "aField",
});
});
describe("supports undefined values inside objects in lists", () => {
expect(() => validator.validate([{ a: [{ a: undefined }] }], "aField")).not.toThrowError();
expect(validator.validate([{ a: [{ a: null }] }, { a: [{ a: undefined }] }], "aField")).toBeNull();
expect(validator.validate([{ a: [{ a: undefined }] }, { a: [{ a: undefined }] }], "aField")).toEqual({
constraintType: "uniqueItems",
failureValue: [{ a: [{ a: undefined }] }],
path: "aField",
});
});
});

0 comments on commit dff4548

Please sign in to comment.