Skip to content
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

Invalid json parsing errors #105

Merged
merged 2 commits into from
Oct 28, 2024
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
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions libs/as-sdk-integration-tests/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
TestHttpSuccess,
TestPostHttpSuccess,
} from "./http";
import { TestInvalidAttribute } from "./invalid-json";
import { TestGenerateProxyMessage, TestProxyHttpFetch } from "./proxy-http";
import { TestTallyVmReveals, TestTallyVmRevealsFiltered } from "./tally";
import { TestTallyVmHttp, TestTallyVmMode } from "./vm-tests";
Expand Down Expand Up @@ -97,6 +98,8 @@ if (args === "testHttpRejection") {
new TestLogNull().run();
} else if (args === "testLogFloat") {
new TestLogFloat().run();
} else if (args === "testInvalidAttribute") {
new TestInvalidAttribute().run();
}

Process.error(Bytes.fromUtf8String("No argument given"));
15 changes: 15 additions & 0 deletions libs/as-sdk-integration-tests/assembly/invalid-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Bytes, OracleProgram, Process } from "../../as-sdk/assembly";

@json
class InvalidAttribute {
value!: string;
}

export class TestInvalidAttribute extends OracleProgram {
execution(): void {
const result =
Bytes.fromUtf8String(`{"value":0}`).toJSON<InvalidAttribute>();

Process.success(Bytes.fromJSON(result));
}
}
23 changes: 23 additions & 0 deletions libs/as-sdk-integration-tests/src/invalid-json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from "bun:test";
import { readFile } from "node:fs/promises";
import { testOracleProgramExecution } from "@seda/dev-tools";

const oracleProgram = await readFile(
"dist/libs/as-sdk-integration-tests/debug.wasm",
);

describe("invalid json", () => {
describe("invalid attribute", () => {
it("should mention the mismatched type", async () => {
const result = await testOracleProgramExecution(
oracleProgram,
Buffer.from("testInvalidAttribute"),
);

expect(result.exitCode).toBe(255);
expect(result.stderr).toContain(
"Mismatched Types! Expected string but got",
);
});
});
});
19 changes: 18 additions & 1 deletion libs/as-sdk/assembly/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ export class Bytes {
return true;
}

/**
* @hidden Required for JSON-AS (de)serialization.
*/
__DESERIALIZE_SAFE(
data: string,
_key_start: i32,
_key_end: i32,
_outerLoopIndex: i32,
_numberValueIndex: i32,
): bool {
const innerBytes = JSON.parseSafe<InnerBytes>(data);
const buffer = decodeHex(innerBytes.value);
this.value = buffer;

return true;
}

/**
* The underlying {@link ArrayBuffer} in case you need to manipulate it directly.
* Most likely {@linkcode toUtf8String} or {@linkcode toHexString} is the better choice.
Expand Down Expand Up @@ -288,7 +305,7 @@ export class Bytes {
* @returns an instance of '@json' annotated class <T>
*/
toJSON<T>(): T {
return JSON.parse<T>(this.toUtf8String());
return JSON.parseSafe<T>(this.toUtf8String());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions libs/as-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@seda-protocol/as-sdk",
"type": "module",
"version": "0.0.15",
"version": "0.0.16",
"types": "./assembly/index.ts",
"dependencies": {
"@assemblyscript/wasi-shim": "git+https://github.com/sedaprotocol/wasi-shim.git",
"as-bignum": "^0.3.1",
"json-as": "^0.9.6",
"json-as": "^0.9.26",
"visitor-as": "^0.11.4"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"cosmjs-types": "^0.9.0",
"dotenv": "^16.3.1",
"figlet": "^1.6.0",
"json-as": "0.9.6",
"json-as": "0.9.26",
"node-fetch": "^3.3.2",
"node-gzip": "^1.1.2",
"ora": "^7.0.1",
Expand Down
Loading