Skip to content

Test if returned result is nullable before encoding it as null #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2019
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
2 changes: 1 addition & 1 deletion bindings/dist/transformerBundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bindings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function __wrapper_${name}(): void {`);
if (toString(returnType) !== "void") {
this.sb.push(`
let encoder = new JSONEncoder();
if (result == null) {
if ((isString<${returnTypeName}>() || isNullable<${returnTypeName}>()) && result == null) {
encoder.setNull(null);
} else {
encode<${returnTypeName}>(encoder, result${hasNull ? "!" : ""}, null);
Expand Down
2 changes: 1 addition & 1 deletion dist/asc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/asc.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/near-bindgen/assembly/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class FooBar {
foo: i32 = 0;
bar: u32 = 1;
u64Val: u64 = 4294967297;
u64_zero: u64;
i64Val: i64 = -64;
flag: bool;
baz: string = "123";
Expand Down
1 change: 1 addition & 0 deletions tests/near-bindgen/assembly/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function runTest(): void {
assert(base64.encode(original.uint8arrays[0]) == base64.encode(decoded.uint8arrays[0]));
assert(original.arr[0][0] == "Hello");
assert(original.u64Arr[0] == decoded.u64Arr[0]);
assert(original.u64_zero == decoded.u64_zero);

const nullable = new Nullables();
//@ts-ignore
Expand Down
8 changes: 4 additions & 4 deletions tests/near-bindgen/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ async function loadModule(path) {
foobars: [{ foo: -12345, bar: 123, flag: true, baz: "bazinga" }] }),
[{ foobar: { foo: -12345, bar: 123, u64Val: (2**32 + 1).toString(),
i64Val: "-64", flag: true, baz: "bazinga", uint8array: null, arr: null,
u32Arr: null, i32Arr: null, u128Val: null, uint8arrays: null, u64Arr: null}}]);
u32Arr: null, i32Arr: null, u128Val: null, uint8arrays: null, u64Arr: null, u64_zero:"0"}}]);
assert.deepEqual(await module.convertFoobars({
foobars: [{ arr: [["1", "2"], ["3"]] }] }),
[{ 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}}]);
[{ 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"}}]);
assert.equal(await module.getStringArrayLength({ arr: ["1", "2", "3"] }), 3);
assert.deepEqual(await module.rewrapFoobar({ container: { foobar: { foo: 123 } } }),
{"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}]]});
{"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"}]]});
assert.deepEqual(await module.unwrapFoobar({ container: { foobars: [[{ foo: 123 }]] } }),
{"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});
{"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"});
assert.deepEqual(await module.stringOrNull(), null)
})().catch(e => {
console.error('Error during test execution:', e);
Expand Down