[C++] Set StructDef::has_key property when deserializing from binary schema (#7386) (#7428)

This commit is contained in:
sssooonnnggg
2022-08-13 00:27:17 +08:00
committed by GitHub
parent fc5d86f1e7
commit 4be605604e
2 changed files with 31 additions and 0 deletions

View File

@@ -3720,6 +3720,14 @@ bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
delete field_def;
return false;
}
if (field_def->key) {
if (has_key) {
// only one field may be set as key
delete field_def;
return false;
}
has_key = true;
}
if (fixed) {
// Recompute padding since that's currently not serialized.
auto size = InlineSize(field_def->value.type);

View File

@@ -4550,6 +4550,28 @@ void PrivateAnnotationsLeaks() {
}
}
void JsonUnsortedArrayTest()
{
flatbuffers::Parser parser;
TEST_EQ(parser.Deserialize(MyGame::Example::MonsterBinarySchema::data(), MyGame::Example::MonsterBinarySchema::size()), true);
auto jsonStr = R"(
{
"name": "lookupTest",
"testarrayoftables": [
{ "name": "aaa" },
{ "name": "ccc" },
{ "name": "bbb" }
]
}
)";
TEST_EQ(parser.ParseJson(jsonStr), true);
auto monster = flatbuffers::GetRoot<MyGame::Example::Monster>(parser.builder_.GetBufferPointer());
TEST_NOTNULL(monster->testarrayoftables()->LookupByKey("aaa"));
TEST_NOTNULL(monster->testarrayoftables()->LookupByKey("bbb"));
TEST_NOTNULL(monster->testarrayoftables()->LookupByKey("ccc"));
}
int FlatBufferTests() {
// clang-format off
@@ -4655,6 +4677,7 @@ int FlatBufferTests() {
WarningsAsErrorsTest();
NestedVerifierTest();
PrivateAnnotationsLeaks();
JsonUnsortedArrayTest();
return 0;
}