Skip to content

[TS] Bug. createX() does not throw if the second of two consecutive required string fields is missing #7739

Closed

Description

flatbuffers version: 22.11.23
OS: MacOS and Linux

If a schema defines two consecutive required string fields, the generated createX() function DOES NOT THROW when the second field is missing.

Further info, builder.requiredFiled gets a non zero value in this line var ok = this.bb.readInt16(vtable_start + field) != 0; and hence it does not throw.

NOTE: If the schema defines two NON consecutive required string fields, the generated createX function THROWS when the first or second string field is missing.

Above the STR demonstrating the bug.

tests.fbs:

namespace FBS.Test;

// Table with two consecutive required string fields.
// - createX() THROWS is null|undefined value is provided as `str_a`.
// - createX() DOES NOT THROW is null|undefined value is provided as `str_b`.
table Foo {
  str_a:string (required);
  str_b:string (required);
}

// Table with two NON consecutive required string fields.
// - createX() THROWS is null|undefined value is provided as `str_a`.
// - createX() THROWS is null|undefined value is provided as `str_b`.
table Bar {
  str_a:string (required);
  int_a:uint8;
  str_b:string (required);
}

test.ts:

import * as flatbuffers from 'flatbuffers';
import * as FbsTest from '../fbs/test_generated';

// flatbuffers builder.
const builder:flatbuffers.Builder = new flatbuffers.Builder(1024);

afterEach(() => builder.clear());

test('createFoo() throws if str_a is not set', () =>
{
	const strA = builder.createString(undefined);
	const strB = builder.createString('foo');

	expect(() => FbsTest.Foo.createFoo(
		builder, strA, strB
	)).toThrow();
});

test('createFoo() throws if str_b is not set', () =>
{
	const strA = builder.createString('foo');
	const strB = builder.createString(undefined);

	expect(() => FbsTest.Foo.createFoo(
		builder, strA, strB
	)).toThrow();
});

test('createBar() throws if str_a is not set', () =>
{
	const strA = builder.createString(undefined);
	const strB = builder.createString('foo');

	expect(() => FbsTest.Bar.createBar(
		builder, strA, 0, strB
	)).toThrow();
});

test('createBar() throws if str_b is not set', () =>
{
	const strA = builder.createString('foo');
	const strB = builder.createString(undefined);

	expect(() => FbsTest.Bar.createBar(
		builder, strA, 0, strB
	)).toThrow();
});

Result of running the test:

Screenshot 2022-12-24 at 15 20 17

Corresponding PR #7752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions