Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Mapster.Tests/WhenMappingToInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace Mapster.Tests
{
Expand Down Expand Up @@ -266,6 +268,27 @@ public void MappingToInteraceWithReadonlyProps_AllPropsInitialized()
);
}

[TestMethod]
public void MappingToInterface_VerifyReadonlyPropsInterfaceRule()
{
SampleInterfaceCls source = new SampleInterfaceCls
{
ActivityData = new SampleActivityData
{
Data = new SampleActivityParsedData
{
Steps = new List<string> { "A", "B", "C" }
}
}
};

SampleInterfaceCls target = source.Adapt<SampleInterfaceCls>();
target.ShouldNotBeNull();
target.ShouldSatisfyAllConditions(
() => target.ActivityData.ShouldBe(source.ActivityData)
);
}

public interface IInheritedDtoWithoutProperties : IInheritedDto
{
}
Expand Down Expand Up @@ -374,6 +397,42 @@ public class PropertyInitializationTestSource
public int Property1 { get; set; }
public int Property2 { get; set; }
}

public interface IActivityData
{

}

public class SampleInterfaceCls
{
[Newtonsoft.Json.JsonIgnore]
public IActivityData? ActivityData { get; set; }

public SampleInterfaceCls()
{

}

public SampleInterfaceCls(IActivityData data)
{
SetActivityData(data);
}

public void SetActivityData(IActivityData data)
{
ActivityData = data;
}
}

public class SampleActivityData : IActivityData
{
public SampleActivityParsedData Data { get; set; }
}

public class SampleActivityParsedData
{
public List<string> Steps { get; set; } = new List<string>();
}

}
}
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/ReadOnlyInterfaceAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class ReadOnlyInterfaceAdapter : ClassAdapter

protected override bool CanMap(PreCompileArgument arg)
{
return arg.DestinationType.IsInterface;
return arg.DestinationType.IsInterface && arg.DestinationType.GetProperties().Length > 0;
}

protected override bool CanInline(Expression source, Expression? destination, CompileArgument arg)
Expand Down
Loading