Skip to content

Commit

Permalink
Fixed issue with TypeConverter being assigned to a member that has a …
Browse files Browse the repository at this point in the history
…Convert expression assigned to it.
  • Loading branch information
JoshClose committed Nov 11, 2022
1 parent d25c426 commit a676f56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CsvHelper/Configuration/ClassMapCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void SetMapDefaults(ClassMap map)
continue;
}

if (memberMap.Data.TypeConverter == null)
if (memberMap.Data.TypeConverter == null && memberMap.Data.ReadingConvertExpression == null && memberMap.Data.WritingConvertExpression == null)
{
memberMap.Data.TypeConverter = context.TypeConverterCache.GetConverter(memberMap.Data.Member.MemberType());
}
Expand Down
34 changes: 34 additions & 0 deletions tests/CsvHelper.Tests/Configuration/CsvClassMapCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using CsvHelper.Configuration;
using Xunit;
using System.Globalization;
using System.Collections;
using System;

namespace CsvHelper.Tests.Configuration
{
Expand All @@ -25,12 +27,44 @@ public void GetChildMapWhenParentIsMappedBeforeIt()
Assert.Equal(childMap, map);
}

[Fact]
public void Add_Enumerable_HasConvertSet_DoesNotAssignTypeConverter()
{
var context = new CsvContext(new CsvConfiguration(CultureInfo.InvariantCulture));
var collection = new ClassMapCollection(context);

var map = new EnumerablePropertyClassMap();
collection.Add(map);
Assert.Null(map.MemberMaps.Find<EnumerablePropertyClass>(m => m.Enumerable).Data.TypeConverter);
}

private class Parent { }

private class Child : Parent { }

private sealed class ParentMap : ClassMap<Parent> { }

private sealed class ChildMap : ClassMap<Child> { }

private class EnumerablePropertyClass
{
public Enumerable Enumerable { get; set; }
}

private class EnumerablePropertyClassMap : ClassMap<EnumerablePropertyClass>
{
public EnumerablePropertyClassMap()
{
Map(m => m.Enumerable).Convert(_ => new Enumerable());
}
}

private class Enumerable : IEnumerable
{
public IEnumerator GetEnumerator()
{
throw new NotSupportedException();
}
}
}
}

0 comments on commit a676f56

Please sign in to comment.