Skip to content

Commit 7711e84

Browse files
authored
Fix TS object API generation of schema containing array of enumeration having no zero default (#8832)
* set the array constructor to fill with minvalue * add regression generation test
1 parent 89430a1 commit 7711e84

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/idl_gen_ts.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,12 @@ class TsGenerator : public BaseGenerator {
505505
std::string enum_name =
506506
AddImport(imports, *value.type.enum_def, *value.type.enum_def)
507507
.name;
508-
std::string enum_value = namer_.Variant(
509-
*value.type.enum_def->FindByValue(value.constant));
508+
const EnumVal* val =
509+
value.type.enum_def->FindByValue(value.constant);
510+
if (val == nullptr) {
511+
val = value.type.enum_def->MinValue();
512+
}
513+
std::string enum_value = namer_.Variant(*val);
510514
ret += enum_name + "." + enum_value +
511515
(i < value.type.fixed_length - 1 ? ", " : "");
512516
}
@@ -521,9 +525,10 @@ class TsGenerator : public BaseGenerator {
521525
return "BigInt('" + value.constant + "')";
522526
}
523527
default: {
524-
EnumVal* val = value.type.enum_def->FindByValue(value.constant);
525-
if (val == nullptr)
526-
val = const_cast<EnumVal*>(value.type.enum_def->MinValue());
528+
const EnumVal* val = value.type.enum_def->FindByValue(value.constant);
529+
if (val == nullptr) {
530+
val = value.type.enum_def->MinValue();
531+
}
527532
return AddImport(imports, *value.type.enum_def, *value.type.enum_def)
528533
.name +
529534
"." + namer_.Variant(*val);

tests/non_zero_enum.fbs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
enum NonZero: ubyte {
2+
VAL = 1,
3+
}
4+
5+
struct NonZeroArrayStruct {
6+
data: [NonZero:4];
7+
}
8+
9+
table NonZeroVectorTable {
10+
values: [NonZero];
11+
value: NonZero = VAL;
12+
}

tests/ts/TypeScriptTest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ def esbuild(input, output):
9494
data="../unicode_test.json",
9595
)
9696

97+
flatc(
98+
options=["--ts", "--gen-object-api"],
99+
schema="../non_zero_enum.fbs",
100+
)
101+
97102
flatc(
98103
options=[
99104
"--ts",

0 commit comments

Comments
 (0)