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

Prevent mapping to inaccessible construtors #456

Merged
merged 1 commit into from
May 23, 2023

Conversation

TimothyMakkison
Copy link
Collaborator

@TimothyMakkison TimothyMakkison commented May 22, 2023

Stop CtorMapping from mapping to inaccessible constructors

Description
CtorMapping will try to construct an object from itself using inaccessible constructors. This usually occurs with records because they all have protected constructors.

[Mapper(UseDeepCloning = true)]
public static partial class Mapper
{
    public static partial MyRecord Map(MyRecord src);
    public static partial MyClass Map(MyClass src);
}
public class MyClass
{
    protected MyClass(MyClass myClass)
    {
    }
}
public record MyRecord
{
    public int Value { get; set; }
}
// Generates
public static partial global::Riok.Mapperly.Sample.MyRecord Map(global::Riok.Mapperly.Sample.MyRecord src)
{
    return new global::Riok.Mapperly.Sample.MyRecord(src); // invalid can't access constructor
}

public static partial global::Riok.Mapperly.Sample.MyClass Map(global::Riok.Mapperly.Sample.MyClass src)
{
    return new global::Riok.Mapperly.Sample.MyClass(src); // invalid can't access constructor
}
  • Added accessibility check to CtorMapping
  • Added unit test

@codecov
Copy link

codecov bot commented May 22, 2023

Codecov Report

Merging #456 (a7aa3cf) into main (b17f666) will increase coverage by 0.14%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #456      +/-   ##
==========================================
+ Coverage   92.08%   92.22%   +0.14%     
==========================================
  Files         135      135              
  Lines        4282     4295      +13     
  Branches      591      590       -1     
==========================================
+ Hits         3943     3961      +18     
+ Misses        225      223       -2     
+ Partials      114      111       -3     
Impacted Files Coverage Δ
.../Descriptors/MappingBuilders/CtorMappingBuilder.cs 73.33% <100.00%> (+4.10%) ⬆️
...iptors/MappingBuilders/EnumerableMappingBuilder.cs 97.57% <100.00%> (+3.06%) ⬆️
...Mapperly/Descriptors/UserMethodMappingExtractor.cs 92.98% <100.00%> (+0.04%) ⬆️
src/Riok.Mapperly/Helpers/SymbolExtensions.cs 89.47% <100.00%> (+0.90%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@latonz latonz enabled auto-merge (squash) May 23, 2023 06:11
@latonz latonz merged commit 125742e into riok:main May 23, 2023
@github-actions
Copy link

🎉 This PR is included in version 2.9.0-next.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Aug 7, 2023

🎉 This PR is included in version 3.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants