Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #755 - Regression in interfaces mapping introduced by #649 #756

Merged
merged 1 commit into from
Jan 13, 2025
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