flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[TS] In TypeScript generated code vectors are defaulted to empty array disregard the specification

Open 3axap4eHko opened this issue 3 years ago • 6 comments

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

3axap4eHko avatar Nov 09 '22 17:11 3axap4eHko

@dbaileychess any thoughts on this?

3axap4eHko avatar Dec 07 '22 21:12 3axap4eHko

I don't, maybe @bjornharrtell does? I don't really know TS.

dbaileychess avatar Dec 08 '22 01:12 dbaileychess

I agree the current behaviour is in error and would welcome a fix.

bjornharrtell avatar Dec 08 '22 07:12 bjornharrtell

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.

github-actions[bot] avatar Jun 08 '23 20:06 github-actions[bot]

any updates?

3axap4eHko avatar Jun 21 '23 17:06 3axap4eHko

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.

github-actions[bot] avatar Dec 21 '23 20:12 github-actions[bot]

This issue was automatically closed due to no activity for 6 months plus the 14 day notice period.

github-actions[bot] avatar Jan 04 '24 20:01 github-actions[bot]