[TS] In TypeScript generated code vectors are defaulted to empty array disregard the specification
According to the specification https://google.github.io/flatbuffers/md__schemas.html
Each field has a name, a type, and optionally a default value (if omitted, it defaults to 0 / NULL).
That brings data inconsistency between the encoding and decoding of vectors in TypeScript since they have defaulted to an empty array.
Steps to reproduce
Prepare a project:
npm init -y && npm i @types/node flatbuffers ts-node typescript && mkdir -p src
Create a file schema.fbs
table Test {
aVector: [string];
}
root_type Test;
Generate a typescript code
./flatc --ts --gen-all --gen-object-api -I ./ -o ./src ./schema.fbs
Create src/index.ts file that generates test.bin
import * as FS from "fs";
import * as FB from "flatbuffers";
import { TestT } from "./test";
const builder = new FB.Builder();
const test = new TestT(); // <- I expect every field must be not defined
const offset = test.pack(builder);
builder.finish(offset);
const buffer = Buffer.from(builder.asUint8Array());
FS.writeFileSync("test.bin", buffer);
Analyze test.bin
hexdump -C test.bin
00000000 0c 00 00 00 00 00 06 00 08 00 04 00 06 00 00 00 |................|
00000010 04 00 00 00 00 00 00 00 |........|
As you can see table is at 0x0C vtable is at 0x06 vtable size is 6 table size is 8 aVector has non-zero offset 0x04
flatc --raw-binary --strict-json -t ./schema.fbs -- ./test.bin
cat test.json
Result
{
"aVector": [
]
}
Expected
{}
P.S. Happy to fix it
@dbaileychess any thoughts on this?
I don't, maybe @bjornharrtell does? I don't really know TS.
I agree the current behaviour is in error and would welcome a fix.
This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.
any updates?
This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.
This issue was automatically closed due to no activity for 6 months plus the 14 day notice period.