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/google/flatbuffers/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 <derekbailey@google.com>
This commit is contained in:
Stefan F
2023-01-07 21:37:22 +01:00
committed by GitHub
parent 06f2a3dce9
commit b5802b57f2
5 changed files with 362 additions and 4 deletions

View File

@@ -407,6 +407,13 @@ type_field_collsion_schema = "type_field_collsion.fbs"
flatc(["--csharp", "--gen-object-api"], schema=type_field_collsion_schema)
# Union / value collision
flatc(
CS_OPTS + ["--gen-object-api", "--gen-onefile"],
prefix="union_value_collsion",
schema="union_value_collision.fbs"
)
# Generate string/vector default code for tests
flatc(RUST_OPTS, prefix="more_defaults", schema="more_defaults.fbs")

View File

@@ -1525,7 +1525,7 @@ class CSharpGenerator : public BaseGenerator {
" _o, "
"Newtonsoft.Json.JsonSerializer serializer) {\n";
code += " if (_o == null) return;\n";
code += " serializer.Serialize(writer, _o.Value);\n";
code += " serializer.Serialize(writer, _o." + class_member + ");\n";
code += " }\n";
code +=
" public override object ReadJson(Newtonsoft.Json.JsonReader "
@@ -1562,8 +1562,8 @@ class CSharpGenerator : public BaseGenerator {
code += " default: break;\n";
} else {
auto type_name = GenTypeGet_ObjectAPI(ev.union_type, opts);
code += " case " + Name(enum_def) + "." + Name(ev) +
": _o.Value = serializer.Deserialize<" + type_name +
code += " case " + Name(enum_def) + "." + Name(ev) + ": _o." +
class_member + " = serializer.Deserialize<" + type_name +
">(reader); break;\n";
}
}
@@ -1586,7 +1586,7 @@ class CSharpGenerator : public BaseGenerator {
auto &code = *code_ptr;
std::string varialbe_name = "_o." + camel_name;
std::string class_member = "Value";
if (class_member == camel_name) class_member += "_";
if (class_member == enum_def.name) class_member += "_";
std::string type_suffix = "";
std::string func_suffix = "()";
std::string indent = " ";

View File

@@ -169,6 +169,9 @@
<Compile Include="..\nested_namespace_test\nested_namespace_test3_generated.cs">
<Link>nested_namespace_test\nested_namespace_test3_generated.cs</Link>
</Compile>
<Compile Include="..\union_value_collsion\union_value_collision_generated.cs">
<Link>union_value_collsion\union_value_collision_generated.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,17 @@
namespace union_value_collsion;
table IntValue {
value:int;
}
union Value { IntValue }
union Other { IntValue }
// This table tests collsions of Unions and fields named value.
table Collision {
some_value : Value;
value : Other;
}
root_type Collision;

View File

@@ -0,0 +1,331 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace union_value_collsion
{
using global::System;
using global::System.Collections.Generic;
using global::Google.FlatBuffers;
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum Value : byte
{
NONE = 0,
IntValue = 1,
};
public class ValueUnion {
public Value Type { get; set; }
public object Value_ { get; set; }
public ValueUnion() {
this.Type = Value.NONE;
this.Value_ = null;
}
public T As<T>() where T : class { return this.Value_ as T; }
public union_value_collsion.IntValueT AsIntValue() { return this.As<union_value_collsion.IntValueT>(); }
public static ValueUnion FromIntValue(union_value_collsion.IntValueT _intvalue) { return new ValueUnion{ Type = Value.IntValue, Value_ = _intvalue }; }
public static int Pack(Google.FlatBuffers.FlatBufferBuilder builder, ValueUnion _o) {
switch (_o.Type) {
default: return 0;
case Value.IntValue: return union_value_collsion.IntValue.Pack(builder, _o.AsIntValue()).Value;
}
}
}
public class ValueUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
public override bool CanConvert(System.Type objectType) {
return objectType == typeof(ValueUnion) || objectType == typeof(System.Collections.Generic.List<ValueUnion>);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = value as System.Collections.Generic.List<ValueUnion>;
if (_olist != null) {
writer.WriteStartArray();
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
writer.WriteEndArray();
} else {
this.WriteJson(writer, value as ValueUnion, serializer);
}
}
public void WriteJson(Newtonsoft.Json.JsonWriter writer, ValueUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return;
serializer.Serialize(writer, _o.Value_);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = existingValue as System.Collections.Generic.List<ValueUnion>;
if (_olist != null) {
for (var _j = 0; _j < _olist.Count; ++_j) {
reader.Read();
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
}
reader.Read();
return _olist;
} else {
return this.ReadJson(reader, existingValue as ValueUnion, serializer);
}
}
public ValueUnion ReadJson(Newtonsoft.Json.JsonReader reader, ValueUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return null;
switch (_o.Type) {
default: break;
case Value.IntValue: _o.Value_ = serializer.Deserialize<union_value_collsion.IntValueT>(reader); break;
}
return _o;
}
}
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum Other : byte
{
NONE = 0,
IntValue = 1,
};
public class OtherUnion {
public Other Type { get; set; }
public object Value { get; set; }
public OtherUnion() {
this.Type = Other.NONE;
this.Value = null;
}
public T As<T>() where T : class { return this.Value as T; }
public union_value_collsion.IntValueT AsIntValue() { return this.As<union_value_collsion.IntValueT>(); }
public static OtherUnion FromIntValue(union_value_collsion.IntValueT _intvalue) { return new OtherUnion{ Type = Other.IntValue, Value = _intvalue }; }
public static int Pack(Google.FlatBuffers.FlatBufferBuilder builder, OtherUnion _o) {
switch (_o.Type) {
default: return 0;
case Other.IntValue: return union_value_collsion.IntValue.Pack(builder, _o.AsIntValue()).Value;
}
}
}
public class OtherUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
public override bool CanConvert(System.Type objectType) {
return objectType == typeof(OtherUnion) || objectType == typeof(System.Collections.Generic.List<OtherUnion>);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = value as System.Collections.Generic.List<OtherUnion>;
if (_olist != null) {
writer.WriteStartArray();
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
writer.WriteEndArray();
} else {
this.WriteJson(writer, value as OtherUnion, serializer);
}
}
public void WriteJson(Newtonsoft.Json.JsonWriter writer, OtherUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return;
serializer.Serialize(writer, _o.Value);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = existingValue as System.Collections.Generic.List<OtherUnion>;
if (_olist != null) {
for (var _j = 0; _j < _olist.Count; ++_j) {
reader.Read();
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
}
reader.Read();
return _olist;
} else {
return this.ReadJson(reader, existingValue as OtherUnion, serializer);
}
}
public OtherUnion ReadJson(Newtonsoft.Json.JsonReader reader, OtherUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return null;
switch (_o.Type) {
default: break;
case Other.IntValue: _o.Value = serializer.Deserialize<union_value_collsion.IntValueT>(reader); break;
}
return _o;
}
}
public struct IntValue : IFlatbufferObject
{
private Table __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_23_1_4(); }
public static IntValue GetRootAsIntValue(ByteBuffer _bb) { return GetRootAsIntValue(_bb, new IntValue()); }
public static IntValue GetRootAsIntValue(ByteBuffer _bb, IntValue obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
public IntValue __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public int Value { get { int o = __p.__offset(4); return o != 0 ? __p.bb.GetInt(o + __p.bb_pos) : (int)0; } }
public static Offset<union_value_collsion.IntValue> CreateIntValue(FlatBufferBuilder builder,
int value = 0) {
builder.StartTable(1);
IntValue.AddValue(builder, value);
return IntValue.EndIntValue(builder);
}
public static void StartIntValue(FlatBufferBuilder builder) { builder.StartTable(1); }
public static void AddValue(FlatBufferBuilder builder, int value) { builder.AddInt(0, value, 0); }
public static Offset<union_value_collsion.IntValue> EndIntValue(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<union_value_collsion.IntValue>(o);
}
public IntValueT UnPack() {
var _o = new IntValueT();
this.UnPackTo(_o);
return _o;
}
public void UnPackTo(IntValueT _o) {
_o.Value = this.Value;
}
public static Offset<union_value_collsion.IntValue> Pack(FlatBufferBuilder builder, IntValueT _o) {
if (_o == null) return default(Offset<union_value_collsion.IntValue>);
return CreateIntValue(
builder,
_o.Value);
}
}
public class IntValueT
{
[Newtonsoft.Json.JsonProperty("value")]
public int Value { get; set; }
public IntValueT() {
this.Value = 0;
}
}
public struct Collision : IFlatbufferObject
{
private Table __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_23_1_4(); }
public static Collision GetRootAsCollision(ByteBuffer _bb) { return GetRootAsCollision(_bb, new Collision()); }
public static Collision GetRootAsCollision(ByteBuffer _bb, Collision obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
public Collision __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public union_value_collsion.Value SomeValueType { get { int o = __p.__offset(4); return o != 0 ? (union_value_collsion.Value)__p.bb.Get(o + __p.bb_pos) : union_value_collsion.Value.NONE; } }
public TTable? SomeValue<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(6); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
public union_value_collsion.IntValue SomeValueAsIntValue() { return SomeValue<union_value_collsion.IntValue>().Value; }
public union_value_collsion.Other ValueType { get { int o = __p.__offset(8); return o != 0 ? (union_value_collsion.Other)__p.bb.Get(o + __p.bb_pos) : union_value_collsion.Other.NONE; } }
public TTable? Value<TTable>() where TTable : struct, IFlatbufferObject { int o = __p.__offset(10); return o != 0 ? (TTable?)__p.__union<TTable>(o + __p.bb_pos) : null; }
public union_value_collsion.IntValue ValueAsIntValue() { return Value<union_value_collsion.IntValue>().Value; }
public static Offset<union_value_collsion.Collision> CreateCollision(FlatBufferBuilder builder,
union_value_collsion.Value some_value_type = union_value_collsion.Value.NONE,
int some_valueOffset = 0,
union_value_collsion.Other value_type = union_value_collsion.Other.NONE,
int valueOffset = 0) {
builder.StartTable(4);
Collision.AddValue(builder, valueOffset);
Collision.AddSomeValue(builder, some_valueOffset);
Collision.AddValueType(builder, value_type);
Collision.AddSomeValueType(builder, some_value_type);
return Collision.EndCollision(builder);
}
public static void StartCollision(FlatBufferBuilder builder) { builder.StartTable(4); }
public static void AddSomeValueType(FlatBufferBuilder builder, union_value_collsion.Value someValueType) { builder.AddByte(0, (byte)someValueType, 0); }
public static void AddSomeValue(FlatBufferBuilder builder, int someValueOffset) { builder.AddOffset(1, someValueOffset, 0); }
public static void AddValueType(FlatBufferBuilder builder, union_value_collsion.Other valueType) { builder.AddByte(2, (byte)valueType, 0); }
public static void AddValue(FlatBufferBuilder builder, int valueOffset) { builder.AddOffset(3, valueOffset, 0); }
public static Offset<union_value_collsion.Collision> EndCollision(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<union_value_collsion.Collision>(o);
}
public static void FinishCollisionBuffer(FlatBufferBuilder builder, Offset<union_value_collsion.Collision> offset) { builder.Finish(offset.Value); }
public static void FinishSizePrefixedCollisionBuffer(FlatBufferBuilder builder, Offset<union_value_collsion.Collision> offset) { builder.FinishSizePrefixed(offset.Value); }
public CollisionT UnPack() {
var _o = new CollisionT();
this.UnPackTo(_o);
return _o;
}
public void UnPackTo(CollisionT _o) {
_o.SomeValue = new union_value_collsion.ValueUnion();
_o.SomeValue.Type = this.SomeValueType;
switch (this.SomeValueType) {
default: break;
case union_value_collsion.Value.IntValue:
_o.SomeValue.Value_ = this.SomeValue<union_value_collsion.IntValue>().HasValue ? this.SomeValue<union_value_collsion.IntValue>().Value.UnPack() : null;
break;
}
_o.Value = new union_value_collsion.OtherUnion();
_o.Value.Type = this.ValueType;
switch (this.ValueType) {
default: break;
case union_value_collsion.Other.IntValue:
_o.Value.Value = this.Value<union_value_collsion.IntValue>().HasValue ? this.Value<union_value_collsion.IntValue>().Value.UnPack() : null;
break;
}
}
public static Offset<union_value_collsion.Collision> Pack(FlatBufferBuilder builder, CollisionT _o) {
if (_o == null) return default(Offset<union_value_collsion.Collision>);
var _some_value_type = _o.SomeValue == null ? union_value_collsion.Value.NONE : _o.SomeValue.Type;
var _some_value = _o.SomeValue == null ? 0 : union_value_collsion.ValueUnion.Pack(builder, _o.SomeValue);
var _value_type = _o.Value == null ? union_value_collsion.Other.NONE : _o.Value.Type;
var _value = _o.Value == null ? 0 : union_value_collsion.OtherUnion.Pack(builder, _o.Value);
return CreateCollision(
builder,
_some_value_type,
_some_value,
_value_type,
_value);
}
}
public class CollisionT
{
[Newtonsoft.Json.JsonProperty("some_value_type")]
private union_value_collsion.Value SomeValueType {
get {
return this.SomeValue != null ? this.SomeValue.Type : union_value_collsion.Value.NONE;
}
set {
this.SomeValue = new union_value_collsion.ValueUnion();
this.SomeValue.Type = value;
}
}
[Newtonsoft.Json.JsonProperty("some_value")]
[Newtonsoft.Json.JsonConverter(typeof(union_value_collsion.ValueUnion_JsonConverter))]
public union_value_collsion.ValueUnion SomeValue { get; set; }
[Newtonsoft.Json.JsonProperty("value_type")]
private union_value_collsion.Other ValueType {
get {
return this.Value != null ? this.Value.Type : union_value_collsion.Other.NONE;
}
set {
this.Value = new union_value_collsion.OtherUnion();
this.Value.Type = value;
}
}
[Newtonsoft.Json.JsonProperty("value")]
[Newtonsoft.Json.JsonConverter(typeof(union_value_collsion.OtherUnion_JsonConverter))]
public union_value_collsion.OtherUnion Value { get; set; }
public CollisionT() {
this.SomeValue = null;
this.Value = null;
}
public static CollisionT DeserializeFromJson(string jsonText) {
return Newtonsoft.Json.JsonConvert.DeserializeObject<CollisionT>(jsonText);
}
public string SerializeToJson() {
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
}
public static CollisionT DeserializeFromBinary(byte[] fbBuffer) {
return Collision.GetRootAsCollision(new ByteBuffer(fbBuffer)).UnPack();
}
public byte[] SerializeToBinary() {
var fbb = new FlatBufferBuilder(0x10000);
Collision.FinishCollisionBuffer(fbb, Collision.Pack(fbb, this));
return fbb.DataBuffer.ToSizedArray();
}
}
}