Skip to content

Commit

Permalink
AVRO-2522: Fix nullable resolution inside lists (apache#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
blachniet committed Oct 6, 2019
1 parent 85ce022 commit bfbd2d1
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lang/csharp/src/apache/main/CodeGen/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,21 +703,21 @@ internal static string getType(Schema schema, bool nullible, ref bool nullibleEn
switch (schema.Tag)
{
case Schema.Type.Null:
return "System.Object";
return typeof(object).ToString();
case Schema.Type.Boolean:
if (nullible) return "System.Nullable<bool>";
if (nullible) return $"System.Nullable<{typeof(bool)}>";
else return typeof(bool).ToString();
case Schema.Type.Int:
if (nullible) return "System.Nullable<int>";
if (nullible) return $"System.Nullable<{typeof(int)}>";
else return typeof(int).ToString();
case Schema.Type.Long:
if (nullible) return "System.Nullable<long>";
if (nullible) return $"System.Nullable<{typeof(long)}>";
else return typeof(long).ToString();
case Schema.Type.Float:
if (nullible) return "System.Nullable<float>";
if (nullible) return $"System.Nullable<{typeof(float)}>";
else return typeof(float).ToString();
case Schema.Type.Double:
if (nullible) return "System.Nullable<double>";
if (nullible) return $"System.Nullable<{typeof(double)}>";
else return typeof(double).ToString();

case Schema.Type.Bytes:
Expand Down
114 changes: 114 additions & 0 deletions lang/csharp/src/apache/test/Specific/EmbeddedGenericsRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ------------------------------------------------------------------------------
// <auto-generated>
// Generated by avrogen, version 1.10.0.0
// Changes to this file may cause incorrect behavior and will be lost if code
// is regenerated
// </auto-generated>
// ------------------------------------------------------------------------------
namespace Avro.Test.Specific
{
using System;
using System.Collections.Generic;
using System.Text;
using Avro;
using Avro.Specific;

public partial class EmbeddedGenericsRecord : ISpecificRecord
{
public static Schema _SCHEMA = Avro.Schema.Parse(@"{""type"":""record"",""name"":""EmbeddedGenericsRecord"",""namespace"":""Avro.Test.Specific"",""fields"":[{""name"":""OptionalInt"",""type"":[""null"",""int""]},{""name"":""OptionalIntList"",""type"":{""type"":""array"",""items"":[""null"",""int""]}},{""name"":""OptionalIntMatrix"",""type"":{""type"":""array"",""items"":{""type"":""array"",""items"":{""type"":""array"",""items"":[""null"",""int""]}}}},{""name"":""IntMatrix"",""type"":{""type"":""array"",""items"":{""type"":""array"",""items"":{""type"":""array"",""items"":""int""}}}}]}");
private System.Nullable<System.Int32> _OptionalInt;
private IList<System.Nullable<System.Int32>> _OptionalIntList;
private IList<IList<IList<System.Nullable<System.Int32>>>> _OptionalIntMatrix;
private IList<IList<IList<System.Int32>>> _IntMatrix;
public virtual Schema Schema
{
get
{
return EmbeddedGenericsRecord._SCHEMA;
}
}
public System.Nullable<System.Int32> OptionalInt
{
get
{
return this._OptionalInt;
}
set
{
this._OptionalInt = value;
}
}
public IList<System.Nullable<System.Int32>> OptionalIntList
{
get
{
return this._OptionalIntList;
}
set
{
this._OptionalIntList = value;
}
}
public IList<IList<IList<System.Nullable<System.Int32>>>> OptionalIntMatrix
{
get
{
return this._OptionalIntMatrix;
}
set
{
this._OptionalIntMatrix = value;
}
}
public IList<IList<IList<System.Int32>>> IntMatrix
{
get
{
return this._IntMatrix;
}
set
{
this._IntMatrix = value;
}
}
public virtual object Get(int fieldPos)
{
switch (fieldPos)
{
case 0: return this.OptionalInt;
case 1: return this.OptionalIntList;
case 2: return this.OptionalIntMatrix;
case 3: return this.IntMatrix;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()");
};
}
public virtual void Put(int fieldPos, object fieldValue)
{
switch (fieldPos)
{
case 0: this.OptionalInt = (System.Nullable<System.Int32>)fieldValue; break;
case 1: this.OptionalIntList = (IList<System.Nullable<System.Int32>>)fieldValue; break;
case 2: this.OptionalIntMatrix = (IList<IList<IList<System.Nullable<System.Int32>>>>)fieldValue; break;
case 3: this.IntMatrix = (IList<IList<IList<System.Int32>>>)fieldValue; break;
default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()");
};
}
}
}
62 changes: 62 additions & 0 deletions lang/csharp/src/apache/test/Specific/SpecificTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using System.CodeDom.Compiler;
using Avro.Specific;
using System.Reflection;
using Avro.Test.Specific;
using System.Collections.Generic;

namespace Avro.Test
{
Expand Down Expand Up @@ -246,6 +248,66 @@ public void TestEnumResolution()
Assert.AreEqual( EnumType.SECOND, rec2.enumType );
}

[Test]
public void TestEmbeddedGenerics()
{
var srcRecord = new EmbeddedGenericsRecord
{
OptionalIntList = new List<int?> { 1, 2, null, 3, null, null },
OptionalIntMatrix = new List<IList<IList<int?>>>
{
new List<IList<int?>>
{
new List<int?> { null, 2, },
new List<int?> { null, null },
},
new List<IList<int?>>
{
new List<int?> { 5, 6, },
},
new List<IList<int?>> { },
},
IntMatrix = new List<IList<IList<int>>>
{
new List<IList<int>>
{
new List<int> { 1, 2, },
new List<int> { 3, 4, },
},
new List<IList<int>>
{
new List<int> { 5, 6, },
},
new List<IList<int>> { },
}
};
var stream = serialize(EmbeddedGenericsRecord._SCHEMA, srcRecord);
var dstRecord = deserialize<EmbeddedGenericsRecord>(stream,
EmbeddedGenericsRecord._SCHEMA, EmbeddedGenericsRecord._SCHEMA);

Assert.NotNull(dstRecord);
Assert.AreEqual(1, dstRecord.OptionalIntList[0]);
Assert.AreEqual(2, dstRecord.OptionalIntList[1]);
Assert.AreEqual(null, dstRecord.OptionalIntList[2]);
Assert.AreEqual(3, dstRecord.OptionalIntList[3]);
Assert.AreEqual(null, dstRecord.OptionalIntList[4]);
Assert.AreEqual(null, dstRecord.OptionalIntList[5]);
Assert.AreEqual(null, dstRecord.OptionalIntMatrix[0][0][0]);
Assert.AreEqual(2, dstRecord.OptionalIntMatrix[0][0][1]);
Assert.AreEqual(null, dstRecord.OptionalIntMatrix[0][1][0]);
Assert.AreEqual(null, dstRecord.OptionalIntMatrix[0][1][1]);
Assert.AreEqual(5, dstRecord.OptionalIntMatrix[1][0][0]);
Assert.AreEqual(6, dstRecord.OptionalIntMatrix[1][0][1]);
Assert.AreEqual(0, dstRecord.OptionalIntMatrix[2].Count);
Assert.AreEqual(1, dstRecord.IntMatrix[0][0][0]);
Assert.AreEqual(2, dstRecord.IntMatrix[0][0][1]);
Assert.AreEqual(3, dstRecord.IntMatrix[0][1][0]);
Assert.AreEqual(4, dstRecord.IntMatrix[0][1][1]);
Assert.AreEqual(5, dstRecord.IntMatrix[1][0][0]);
Assert.AreEqual(6, dstRecord.IntMatrix[1][0][1]);
Assert.AreEqual(0, dstRecord.IntMatrix[2].Count);
}

private static S deserialize<S>(Stream ms, Schema ws, Schema rs) where S : class, ISpecificRecord
{
long initialPos = ms.Position;
Expand Down

0 comments on commit bfbd2d1

Please sign in to comment.