Skip to content

When the destination type is a record any properties which are all uppercase are not mapped #805

@chunty

Description

@chunty

Unless you specify a naming strategy mapping to a record with properties all in upper case the destination value is not set. Works fine if the destination type is a class.

Example here:

using System;
using Mapster;
					
public class Program
{
	public static void Main()
	{
		//TypeAdapterConfig.GlobalSettings.Default.NameMatchingStrategy(NameMatchingStrategy.IgnoreCase); // Needed to allow all upper case when mapping to a record
		
		var testClass = new SrcClass();
		var testRecord = new SrcRecord();
		
		LogAdaptResult<DestinationClass>(testClass);
		LogAdaptResult<DestinationClass>(testRecord);
		
		LogAdaptResult<DestinationRecord>(testClass);
		LogAdaptResult<DestinationRecord>(testRecord);		
	}
	
	public static void LogAdaptResult<TOut>(ITestObject obj) where TOut : class, IResult
	{
		var result = obj.Adapt<TOut>();
		Console.WriteLine("-------");
		Console.WriteLine("Mapping {0} to {1}", obj.GetType().Name, result.GetType().Name);
		Console.WriteLine(" - Name: {0}", result.Name);
		Console.WriteLine(" - SPN: {0}", result.SPN);	
		Console.WriteLine(" - UPC: {0}", result.UPC);	
	}
	
	public interface ITestObject
	{
		public string Name {get;}
		public string SPN {get;}
        public string UPC {get;}
	};
	
	public interface IResult
	{
		public string Name {get;}
		public string SPN {get;}
        public string UPC {get;}
	};	
		
	public class SrcClass : ITestObject
	{
		public string Name {get; set;} = "This is class name";
		public string SPN {get; set;} = "This is class SPN";
        public string UPC {get; set;} = "This is class UPC";		
	}
	
	public record SrcRecord(
		string Name = "This is a record name", 
		string SPN = "This is a record SPN",
		string UPC = "This is a record UPC"
	) : ITestObject ;
	
	public class DestinationClass : IResult
	{
		public string Name {get; set;} = string.Empty;
		public string SPN {get; set;} = string.Empty;
		public string UPC {get; set;} = string.Empty;
	}
		
	public record DestinationRecord(
		string Name = "", 
		string SPN = "",
		string UPC = "") : IResult;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions