Skip to content

Commit

Permalink
Merge pull request #452 from multiversx/serializer-null-or-undefined
Browse files Browse the repository at this point in the history
Native serializer: for option and optional, handle both "undefined" and "null" as missing values
  • Loading branch information
andreibancioiu authored May 27, 2024
2 parents a62a324 + 4604253 commit 5d47501
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/smartcontracts/nativeSerializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,34 @@ describe("test native serializer", () => {
assert.deepEqual(typedValues[1].valueOf(), []);
});

it("should accept null or undefined for option types and optionals", async () => {
const endpoint = AbiRegistry.create({
endpoints: [
{
name: "foo",
inputs: [
{
type: "Option<bytes>",
},
{
type: "optional<u32>",
},
],
outputs: [],
},
],
}).getEndpoint("foo");

const typedValuesUsingNull = NativeSerializer.nativeToTypedValues([null, null], endpoint);
const typedValuesUsingUndefined = NativeSerializer.nativeToTypedValues([undefined, undefined], endpoint);

assert.deepEqual(typedValuesUsingNull, typedValuesUsingUndefined);
assert.deepEqual(typedValuesUsingNull[0].getType(), new OptionType(new NullType()));
assert.deepEqual(typedValuesUsingNull[0].valueOf(), null);
assert.deepEqual(typedValuesUsingNull[1].getType(), new OptionalType(new U32Type()));
assert.deepEqual(typedValuesUsingNull[1].valueOf(), null);
});

it("should perform type inference (enums)", async () => {
const abiRegistry = AbiRegistry.create({
endpoints: [
Expand Down
4 changes: 2 additions & 2 deletions src/smartcontracts/nativeSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ export namespace NativeSerializer {
}

function toOptionValue(native: any, type: Type, errorContext: ArgumentErrorContext): TypedValue {
if (native == null) {
if (native == null || native === undefined) {
return OptionValue.newMissing();
}
let converted = convertToTypedValue(native, type.getFirstTypeParameter(), errorContext);
return OptionValue.newProvided(converted);
}

function toOptionalValue(native: any, type: Type, errorContext: ArgumentErrorContext): TypedValue {
if (native == null) {
if (native == null || native === undefined) {
return new OptionalValue(type);
}
let converted = convertToTypedValue(native, type.getFirstTypeParameter(), errorContext);
Expand Down

0 comments on commit 5d47501

Please sign in to comment.