Skip to content

Commit 3cad044

Browse files
Merge pull request #61 from nearprotocol/fix_null_check
Test if returned result is nullable before encoding it as null
2 parents 00f39d2 + ed5a6e1 commit 3cad044

File tree

7 files changed

+10
-8
lines changed

7 files changed

+10
-8
lines changed

bindings/dist/transformerBundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function __wrapper_${name}(): void {`);
206206
if (toString(returnType) !== "void") {
207207
this.sb.push(`
208208
let encoder = new JSONEncoder();
209-
if (result == null) {
209+
if ((isString<${returnTypeName}>() || isNullable<${returnTypeName}>()) && result == null) {
210210
encoder.setNull(null);
211211
} else {
212212
encode<${returnTypeName}>(encoder, result${hasNull ? "!" : ""}, null);

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/near-bindgen/assembly/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class FooBar {
88
foo: i32 = 0;
99
bar: u32 = 1;
1010
u64Val: u64 = 4294967297;
11+
u64_zero: u64;
1112
i64Val: i64 = -64;
1213
flag: bool;
1314
baz: string = "123";

tests/near-bindgen/assembly/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function runTest(): void {
3333
assert(base64.encode(original.uint8arrays[0]) == base64.encode(decoded.uint8arrays[0]));
3434
assert(original.arr[0][0] == "Hello");
3535
assert(original.u64Arr[0] == decoded.u64Arr[0]);
36+
assert(original.u64_zero == decoded.u64_zero);
3637

3738
const nullable = new Nullables();
3839
//@ts-ignore

tests/near-bindgen/test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ async function loadModule(path) {
132132
foobars: [{ foo: -12345, bar: 123, flag: true, baz: "bazinga" }] }),
133133
[{ foobar: { foo: -12345, bar: 123, u64Val: (2**32 + 1).toString(),
134134
i64Val: "-64", flag: true, baz: "bazinga", uint8array: null, arr: null,
135-
u32Arr: null, i32Arr: null, u128Val: null, uint8arrays: null, u64Arr: null}}]);
135+
u32Arr: null, i32Arr: null, u128Val: null, uint8arrays: null, u64Arr: null, u64_zero:"0"}}]);
136136
assert.deepEqual(await module.convertFoobars({
137137
foobars: [{ arr: [["1", "2"], ["3"]] }] }),
138-
[{ foobar: { foo: 0, bar: 1, u64Val: "4294967297", i64Val: "-64", flag: false, baz: '123', uint8array: null, arr: [["1", "2"], ["3"]], u32Arr: null, i32Arr: null, u128Val: null, uint8arrays: null, u64Arr: null}}]);
138+
[{ foobar: { foo: 0, bar: 1, u64Val: "4294967297", i64Val: "-64", flag: false, baz: '123', uint8array: null, arr: [["1", "2"], ["3"]], u32Arr: null, i32Arr: null, u128Val: null, uint8arrays: null, u64Arr: null, u64_zero:"0"}}]);
139139
assert.equal(await module.getStringArrayLength({ arr: ["1", "2", "3"] }), 3);
140140
assert.deepEqual(await module.rewrapFoobar({ container: { foobar: { foo: 123 } } }),
141-
{"foobars":[[{"foo":123,"bar":1,"u64Val":"4294967297","i64Val":"-64","flag":false,"baz":"123","uint8array":null,"arr":null,"u32Arr":null,"i32Arr":null,"u128Val":null,"uint8arrays":null, "u64Arr":null}]]});
141+
{"foobars":[[{"foo":123,"bar":1,"u64Val":"4294967297","i64Val":"-64","flag":false,"baz":"123","uint8array":null,"arr":null,"u32Arr":null,"i32Arr":null,"u128Val":null,"uint8arrays":null, "u64Arr":null, u64_zero:"0"}]]});
142142
assert.deepEqual(await module.unwrapFoobar({ container: { foobars: [[{ foo: 123 }]] } }),
143-
{"foo":123,"bar":1,"u64Val":"4294967297","i64Val":"-64","flag":false,"baz":"123","uint8array":null,"arr":null,"u32Arr":null,"i32Arr":null,"u128Val":null,"uint8arrays":null, u64Arr: null});
143+
{"foo":123,"bar":1,"u64Val":"4294967297","i64Val":"-64","flag":false,"baz":"123","uint8array":null,"arr":null,"u32Arr":null,"i32Arr":null,"u128Val":null,"uint8arrays":null, u64Arr: null, u64_zero:"0"});
144144
assert.deepEqual(await module.stringOrNull(), null)
145145
})().catch(e => {
146146
console.error('Error during test execution:', e);

0 commit comments

Comments
 (0)