MapsterMapper / MapsterMapper/Mapster

[Mapster Tool] Add support for global:: to disambiguate type names in generated mappers

Open
#1,020 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

improvement
Dominant language
C#
Stars
5.2k
Forks
410
Avg merge
2d 12h
Merged PRs (30d)
6

Description

In some cases it could be useful to generate sources where type names are fully qualified with global:: prefix.

If the mapper class is generated in a namespace that partially collides with the one where models are defined, compilation fails.
An example:

namespace Company.Models
{
    public class MyModel
    {
    }
}

namespace ThirdParty.Models
{
    public class OtherModel
    {
    }
}

namespace Prefix.Company.Mappers
{
    using ThirdParty.Models;
    using Company.Models;

    public partial class Mapper
    {
        MyModel Map(OtherModel model);
    }
}

In this case the compiler will trigger a CS0234
The type or namespace 'Models' does not exist in the namespace 'Prefix.Company' (are you missing an assembly reference?)
due to the standard namespace resolution strategy of the compiler.
Even qualifying the name like this

namespace Prefix.Company.Mappers
{
    using ThirdParty.Models;

    public partial class Mapper
    {
        Company.Models.MyModel Map(OtherModel model);
    }
}

does not fix the error, and the only proper fix is to use the global:: qualifier

namespace Prefix.Company.Mappers
{
    using ThirdParty.Models;
    // The following is equivalent to inline global qualificatio
    // using global::Company.Models;

    public partial class Mapper
    {
        global::Company.Models.MyModel Map(OtherModel model);
    }
}

The Mapster tool allows to generate mappers with a fully qualified name, but it does not prefix types with global:: (neither provides an option to do so).
It would be nice if such feature was added.
I am willing to provide a PR eventually.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Mapster tool's existing support for generating fully qualified names and trace how generated type names are emitted. Add an option for the global:: qualifier, then verify that generated mappers compile when model namespaces collide with the mapper namespace.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.