Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

When generating code using Mapster.Tool, if a property is of type Array then the generated type is not mapped properly

Open scottcollins opened this issue 4 years ago • 0 comments

I have two domain records

    public record Province
    {
        public string Name { get; init; } = null!;
        public string Abbreviation { get; init; } = null!;
    }

    public record Country
    {
        public string Name { get; init; } = null!;
        public string Abbreviation { get; init; } = null!;
        public Province[] Provinces { get; init; } = null!;
    }

When using Mapster.Tool to generate the code I get the following generated Data records

    public partial record ProvinceData
    {
        public string Name { get; set; }
        public string Abbreviation { get; set; }
    }

    public partial record CountryData
    {
        public string Name { get; set; }
        public string Abbreviation { get; set; }
        public Province[] Provinces { get; set; }
    }

Notice that the Provinces property of the class CountryData refers to the original Domain record type (Province) and not the generated ProvinceData record type

However, if I change the Domain object to use an ICollection<Province> instead of Province[], the generated code is correct

    public record Province
    {
        public string Name { get; init; } = null!;
        public string Abbreviation { get; init; } = null!;
    }

    public record Country
    {
        public string Name { get; init; } = null!;
        public string Abbreviation { get; init; } = null!;
        public ICollection<Province> Provinces { get; init; } = null!;
    }

Generated Code:

    public partial record ProvinceData
    {
        public string Name { get; set; }
        public string Abbreviation { get; set; }
    }

    public partial record CountryData
    {
        public string Name { get; set; }
        public string Abbreviation { get; set; }
        public ICollection<ProvinceData> Provinces { get; set; }
    }

scottcollins avatar Jun 17 '21 15:06 scottcollins