Skip to content

Commit

Permalink
fix regression in interfaces mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lofcz committed Jan 7, 2025
1 parent c620ed9 commit 2c430e9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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

0 comments on commit 2c430e9

Please sign in to comment.