Skip to content

Commit

Permalink
Fix/146 (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianStrugala authored Feb 15, 2024
1 parent 18e595c commit cb4b600
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/AvroConvert/AvroObjectServices/Read/Resolvers/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal object ResolveArray(TypeSchema writerSchema, TypeSchema readerSchema, I
var typeHash = type.GetHashCode();

int capacity = itemsCount == 0 ? (int)reader.ReadArrayStart() : (int)itemsCount;
Func <IList> resultFunc;
Func<IList> resultFunc;
if (_cachedArrayInitializers.TryGetValue(typeHash, out var initializer))
{
resultFunc = initializer;
Expand Down Expand Up @@ -98,7 +98,7 @@ internal object ResolveArray(TypeSchema writerSchema, TypeSchema readerSchema, I
return resultArray;
}

if (type.IsList())
if (type.IsList() || type.IsEnumerable())
{
return result;
}
Expand Down
14 changes: 12 additions & 2 deletions src/AvroConvert/Infrastructure/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,15 @@ public static Type GetEnumeratedType(this Type type)
return type?.GetElementType() ?? type.GenericTypeArguments.FirstOrDefault();

}

public static Type FindEnumerableType(this Type type)
{
return type?
if (type.IsEnumerable())
{
return type;
}

return type
.GetAllInterfaces()
.SingleOrDefault(t => t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
}
Expand Down Expand Up @@ -417,6 +422,11 @@ internal static bool IsList(this Type type)
return typeof(IList).IsAssignableFrom(type);
}

internal static bool IsEnumerable(this Type type)
{
return type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(IEnumerable<>);
}

internal static bool IsGuid(this Type type)
{
return type == typeof(Guid);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using SolTechnology.Avro;
using Xunit;

namespace AvroConvertComponentTests.Headless.Proto
namespace AvroConvertComponentTests.FilesDeserialization.Headless
{
// from issue: https://github.com/AdrianStrugala/AvroConvert/issues/95
public class HeadlessProtoFileTest
{
[Fact]
[Trait("Fix", "https://github.com/AdrianStrugala/AvroConvert/issues/95")]
public void Component_SerializeHeadlessBiggerObjectAndReadSmaller_NoError()
{
//Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public void List_of_class(Func<object, Type, dynamic> engine)
Assert.Equal(someTestClasses, deserialized);
}

[Theory]
[Trait("Fix", "https://github.com/AdrianStrugala/AvroConvert/issues/146")]
[MemberData(nameof(TestEngine.All), MemberType = typeof(TestEngine))]
public void IEnumerable_of_class(Func<object, Type, dynamic> engine)
{
//Arrange
var someTestClasses = _fixture.CreateMany<BaseTestClass>();

//Act
var deserialized = engine.Invoke(someTestClasses, typeof(IEnumerable<BaseTestClass>));

//Assert
Assert.NotNull(deserialized);
Assert.Equal(someTestClasses, deserialized);
}

[Theory]
[MemberData(nameof(TestEngine.All), MemberType = typeof(TestEngine))]
public void Class_with_list(Func<object, Type, dynamic> engine)
Expand Down

0 comments on commit cb4b600

Please sign in to comment.