mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
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
This commit is contained in:
@@ -505,8 +505,12 @@ class TsGenerator : public BaseGenerator {
|
||||
std::string enum_name =
|
||||
AddImport(imports, *value.type.enum_def, *value.type.enum_def)
|
||||
.name;
|
||||
std::string enum_value = namer_.Variant(
|
||||
*value.type.enum_def->FindByValue(value.constant));
|
||||
const EnumVal* val =
|
||||
value.type.enum_def->FindByValue(value.constant);
|
||||
if (val == nullptr) {
|
||||
val = value.type.enum_def->MinValue();
|
||||
}
|
||||
std::string enum_value = namer_.Variant(*val);
|
||||
ret += enum_name + "." + enum_value +
|
||||
(i < value.type.fixed_length - 1 ? ", " : "");
|
||||
}
|
||||
@@ -521,9 +525,10 @@ class TsGenerator : public BaseGenerator {
|
||||
return "BigInt('" + value.constant + "')";
|
||||
}
|
||||
default: {
|
||||
EnumVal* val = value.type.enum_def->FindByValue(value.constant);
|
||||
if (val == nullptr)
|
||||
val = const_cast<EnumVal*>(value.type.enum_def->MinValue());
|
||||
const EnumVal* val = value.type.enum_def->FindByValue(value.constant);
|
||||
if (val == nullptr) {
|
||||
val = value.type.enum_def->MinValue();
|
||||
}
|
||||
return AddImport(imports, *value.type.enum_def, *value.type.enum_def)
|
||||
.name +
|
||||
"." + namer_.Variant(*val);
|
||||
|
||||
12
tests/non_zero_enum.fbs
Normal file
12
tests/non_zero_enum.fbs
Normal file
@@ -0,0 +1,12 @@
|
||||
enum NonZero: ubyte {
|
||||
VAL = 1,
|
||||
}
|
||||
|
||||
struct NonZeroArrayStruct {
|
||||
data: [NonZero:4];
|
||||
}
|
||||
|
||||
table NonZeroVectorTable {
|
||||
values: [NonZero];
|
||||
value: NonZero = VAL;
|
||||
}
|
||||
@@ -94,6 +94,11 @@ flatc(
|
||||
data="../unicode_test.json",
|
||||
)
|
||||
|
||||
flatc(
|
||||
options=["--ts", "--gen-object-api"],
|
||||
schema="../non_zero_enum.fbs",
|
||||
)
|
||||
|
||||
flatc(
|
||||
options=[
|
||||
"--ts",
|
||||
|
||||
Reference in New Issue
Block a user