mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-04 05:44:13 +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 =
|
std::string enum_name =
|
||||||
AddImport(imports, *value.type.enum_def, *value.type.enum_def)
|
AddImport(imports, *value.type.enum_def, *value.type.enum_def)
|
||||||
.name;
|
.name;
|
||||||
std::string enum_value = namer_.Variant(
|
const EnumVal* val =
|
||||||
*value.type.enum_def->FindByValue(value.constant));
|
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 +
|
ret += enum_name + "." + enum_value +
|
||||||
(i < value.type.fixed_length - 1 ? ", " : "");
|
(i < value.type.fixed_length - 1 ? ", " : "");
|
||||||
}
|
}
|
||||||
@@ -521,9 +525,10 @@ class TsGenerator : public BaseGenerator {
|
|||||||
return "BigInt('" + value.constant + "')";
|
return "BigInt('" + value.constant + "')";
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
EnumVal* val = value.type.enum_def->FindByValue(value.constant);
|
const EnumVal* val = value.type.enum_def->FindByValue(value.constant);
|
||||||
if (val == nullptr)
|
if (val == nullptr) {
|
||||||
val = const_cast<EnumVal*>(value.type.enum_def->MinValue());
|
val = value.type.enum_def->MinValue();
|
||||||
|
}
|
||||||
return AddImport(imports, *value.type.enum_def, *value.type.enum_def)
|
return AddImport(imports, *value.type.enum_def, *value.type.enum_def)
|
||||||
.name +
|
.name +
|
||||||
"." + namer_.Variant(*val);
|
"." + 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",
|
data="../unicode_test.json",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
flatc(
|
||||||
|
options=["--ts", "--gen-object-api"],
|
||||||
|
schema="../non_zero_enum.fbs",
|
||||||
|
)
|
||||||
|
|
||||||
flatc(
|
flatc(
|
||||||
options=[
|
options=[
|
||||||
"--ts",
|
"--ts",
|
||||||
|
|||||||
Reference in New Issue
Block a user