Skip to content

Commit b5802b5

Browse files
Fix [C#] Object API - Invalid Property Name used in UnPackTo for unio… (#7751)
* Fix [C#] Object API - Invalid Property Name used in UnPackTo for union fieldhttps://github.com//issues/7750, also fixes invalid Code generated in WriteJson for Unions named Value. * Test added: new schema union_value_collision.fbs with a Union named Value and a union field named value. The generated C# code now compiles when NetTest.bat. The Code generated with an older flatc.exe didn't compile because of a mismatch of the property name (Value vs. Value_). * branch was not up-to-date with master * BASE_OPTS + CPP_OPTS removed and union_value_collision_generated.h deleted Co-authored-by: Derek Bailey <[email protected]>
1 parent 06f2a3d commit b5802b5

File tree

5 files changed

+362
-4
lines changed

5 files changed

+362
-4
lines changed

scripts/generate_code.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,13 @@ def glob(path, pattern):
407407

408408
flatc(["--csharp", "--gen-object-api"], schema=type_field_collsion_schema)
409409

410+
# Union / value collision
411+
flatc(
412+
CS_OPTS + ["--gen-object-api", "--gen-onefile"],
413+
prefix="union_value_collsion",
414+
schema="union_value_collision.fbs"
415+
)
416+
410417
# Generate string/vector default code for tests
411418
flatc(RUST_OPTS, prefix="more_defaults", schema="more_defaults.fbs")
412419

src/idl_gen_csharp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ class CSharpGenerator : public BaseGenerator {
15251525
" _o, "
15261526
"Newtonsoft.Json.JsonSerializer serializer) {\n";
15271527
code += " if (_o == null) return;\n";
1528-
code += " serializer.Serialize(writer, _o.Value);\n";
1528+
code += " serializer.Serialize(writer, _o." + class_member + ");\n";
15291529
code += " }\n";
15301530
code +=
15311531
" public override object ReadJson(Newtonsoft.Json.JsonReader "
@@ -1562,8 +1562,8 @@ class CSharpGenerator : public BaseGenerator {
15621562
code += " default: break;\n";
15631563
} else {
15641564
auto type_name = GenTypeGet_ObjectAPI(ev.union_type, opts);
1565-
code += " case " + Name(enum_def) + "." + Name(ev) +
1566-
": _o.Value = serializer.Deserialize<" + type_name +
1565+
code += " case " + Name(enum_def) + "." + Name(ev) + ": _o." +
1566+
class_member + " = serializer.Deserialize<" + type_name +
15671567
">(reader); break;\n";
15681568
}
15691569
}
@@ -1586,7 +1586,7 @@ class CSharpGenerator : public BaseGenerator {
15861586
auto &code = *code_ptr;
15871587
std::string varialbe_name = "_o." + camel_name;
15881588
std::string class_member = "Value";
1589-
if (class_member == camel_name) class_member += "_";
1589+
if (class_member == enum_def.name) class_member += "_";
15901590
std::string type_suffix = "";
15911591
std::string func_suffix = "()";
15921592
std::string indent = " ";

tests/FlatBuffers.Test/FlatBuffers.Core.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@
169169
<Compile Include="..\nested_namespace_test\nested_namespace_test3_generated.cs">
170170
<Link>nested_namespace_test\nested_namespace_test3_generated.cs</Link>
171171
</Compile>
172+
<Compile Include="..\union_value_collsion\union_value_collision_generated.cs">
173+
<Link>union_value_collsion\union_value_collision_generated.cs</Link>
174+
</Compile>
172175
</ItemGroup>
173176

174177
<ItemGroup>

tests/union_value_collision.fbs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace union_value_collsion;
2+
3+
table IntValue {
4+
value:int;
5+
}
6+
7+
union Value { IntValue }
8+
9+
union Other { IntValue }
10+
11+
// This table tests collsions of Unions and fields named value.
12+
table Collision {
13+
some_value : Value;
14+
value : Other;
15+
}
16+
17+
root_type Collision;

0 commit comments

Comments
 (0)