From d7070fd71ef8524bdc47c0c84afe0a62ae80dca6 Mon Sep 17 00:00:00 2001 From: Akim <59872966+akim-bow@users.noreply.github.com> Date: Fri, 9 Feb 2024 17:19:56 +0700 Subject: [PATCH] fix(js-client): Handle null as user input value (#431) Handle null as user input value --- .../src/compilerSupport/__test__/conversion.spec.ts | 7 ++++++- packages/core/js-client/src/compilerSupport/conversions.ts | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/core/js-client/src/compilerSupport/__test__/conversion.spec.ts b/packages/core/js-client/src/compilerSupport/__test__/conversion.spec.ts index fef9ed24..946432b7 100644 --- a/packages/core/js-client/src/compilerSupport/__test__/conversion.spec.ts +++ b/packages/core/js-client/src/compilerSupport/__test__/conversion.spec.ts @@ -179,6 +179,7 @@ describe("Conversion from aqua to typescript", () => { test.each` aqua | ts | type ${1} | ${1} | ${i32} + ${null} | ${null} | ${opt_i32} ${[]} | ${null} | ${opt_i32} ${[1]} | ${1} | ${opt_i32} ${[1, 2, 3]} | ${[1, 2, 3]} | ${array_i32} @@ -205,7 +206,11 @@ describe("Conversion from aqua to typescript", () => { // assert expect(tsFromAqua).toStrictEqual(ts); - expect(aquaFromTs).toStrictEqual(aqua); + + // 'null' -> 'null' -> [] ; 'null' not equal [] + if (aqua !== null || ts !== null) { + expect(aquaFromTs).toStrictEqual(aqua); + } }, ); }); diff --git a/packages/core/js-client/src/compilerSupport/conversions.ts b/packages/core/js-client/src/compilerSupport/conversions.ts index db30dffa..68ce8865 100644 --- a/packages/core/js-client/src/compilerSupport/conversions.ts +++ b/packages/core/js-client/src/compilerSupport/conversions.ts @@ -102,11 +102,11 @@ export function aqua2js( if (schema.tag === "nil") { return null; } else if (schema.tag === "option") { - if (!Array.isArray(value)) { - throw new SchemaValidationError(path, schema, "array", value); + if (!Array.isArray(value) && value !== null) { + throw new SchemaValidationError(path, schema, "array or null", value); } - if ("0" in value) { + if (value !== null && "0" in value) { return aqua2js(value[0], schema.type, { path: [...path, "?"] }); } else { return null;