Skip to content

Improvements for Mapster Tool - #1011

Merged
DocSvartz merged 9 commits into
MapsterMapper:developmentfrom
DocSvartz:feat-MapsterTool--publish
Aug 25, 2026
Merged

Improvements for Mapster Tool #1011
DocSvartz merged 9 commits into
MapsterMapper:developmentfrom
DocSvartz:feat-MapsterTool--publish

Conversation

@DocSvartz

@DocSvartzDocSvartz commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Improvements for generating mappers from interface :

  1. Added support mappers that use internal methods or properties.
  2. Added support for marking generated mapper attributes [MapsterToolGeneratedMapper]
    new Mapster.Tool command mapper option:
    • -h: true - Create a mapper with helpers attributes
    • -H: - add additional namespace for generated helpers attributes

Note

By default helpers attributes generate in namespace Mapster.Generated.Attributes.{Filename of assembly} specified in the -a option.
-H: "MyNameSpace" - create next namespace Mapster.Generated.Attributes.MyNameSpace
If assembly filename or the string passed as a parameter -H is not a valid C# namespace, a additional namespace will be created automatically.

@DocSvartz

This comment was marked as resolved.

@DocSvartz

DocSvartz commented Aug 20, 2026

Copy link
Copy Markdown
ContributorAuthor

@andrerav
That is, if you emulate a cases that is not currently supported:

 public interface IMyTypeMapper
{
internal AddressDTO Map(Address p1);
internal Expression<Func<AddressDTO, Address>> Projection { get; }
}

or

internal interface IMyTypeMapper
{
internal AddressDTO Map(Address p1);
internal Expression<Func<AddressDTO, Address>> Projection { get; }
}

emulation step

  1. replace Address to internal

internal class Address

  1. in TypeDefinitions set

IsInternal = false,

we get valid mapper :

public partial class CustomerMapper : IMyTypeMapper {
internal AddressDTO Map(Address p1)
{
return p1 == null ? null : new AddressDTO()
{
Id = p1.Id,
City = p1.City,
Country = p1.Country
};
}
AddressDTO TemplateTest.IMyTypeMapper.Map(Address p1)
{
return p1 == null ? null : new AddressDTO()
{
Id = p1.Id,
City = p1.City,
Country = p1.Country
};
}
internal Expression<Func<AddressDTO, Address>> Projection => p2 => new Address()
{
Id = p2.Id,
City = p2.City,
Country = p2.Country
};
Expression<Func<AddressDTO, Address>> TemplateTest.IMyTypeMapper.Projection => p2 => new Address()
{
Id = p2.Id,
City = p2.City,
Country = p2.Country
};
}

I thought this shouldn't work, but the assembly with this class compiles and the mapper itself works when cast to the interface.🤔

…ic interfaces with internal member
and test refactoring
@DocSvartz

DocSvartz commented Aug 20, 2026

Copy link
Copy Markdown
ContributorAuthor

@andrerav If I'm not mistaken in the current implementation😃

Now mapper members are created correctly depending on the public/internal access modifiers specified for it or using for maping internal clases.

And IsInternal continues to work as before, forcing the creation of an internal mapper with an internal members.

1) property or method maks as public
2) property or method using only public clases
3) not using IsInternal atrribute
@andrerav

Copy link
Copy Markdown
Member

@DocSvartz Your approach is more general and solves both #399 and more. I closed my own PR, which had issues as you pointed out.

@DocSvartzDocSvartz changed the title feat mapster tool Improvements for Mapster Tool Aug 24, 2026
@DocSvartz

Copy link
Copy Markdown
ContributorAuthor

@andrerav Perhaps it's worth adding [MapsterToolGeneratedMapper] to Mapster.Core instead of generating it for each dotnet mapster mapper call?

Due to the use of the [Mapper] attribute on the interface, it is required to use at least Mapster.Core.

- Added feature to deterministically generate a valid namespace based on an invalid string
@DocSvartz

Copy link
Copy Markdown
ContributorAuthor

@andrerav Perhaps it's worth adding [MapsterToolGeneratedMapper] to Mapster.Core instead of generating it for each dotnet mapster mapper call?

Due to the use of the [Mapper] attribute on the interface, it is required to use at least Mapster.Core.

I'm merging this for the pre-release publish. If needed, it can be change in the release.

@DocSvartz
DocSvartz merged commit 4c45b1e into MapsterMapper:developmentAug 25, 2026
2 checks passed
Sign up for freeto 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.

mapster tool generates non-public interface method/property implementation

2 participants

@DocSvartz@andrerav