MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.3k stars 327 forks source link

NullReferenceException #722

Open ljf88 opened 1 month ago

ljf88 commented 1 month ago

Use Version:7.4.0

Source Class:
public class BaseResponse
{
   public string? TraceId { get; set; }
    public Guid InvoiceId { get; set; }
    public GatewayActionResultEnum ActionResult { get; set; }
    public string? ActionResultDescription { get; set; }
    public string? UniqueId { get; set; }
    public BaseRequest RawRequest { get; set; }
    public object RawResponse { get; set; }
    public dynamic? ResponseEntity { get; set; }
    public HttpStatusCode? StatusCode { get; set; }
    public List<Error> Errors { get; set; } = [];
    public bool IsRetry { get; set; }
}

public record Error(string Description, string? Code, string? Name);

public class PrepareResponse : BaseResponse
{
}

Target Class: public class BaseRes { public Guid InvoiceId { get; set; } public GatewayActionResultEnum ActionResult { get; set; } public string? ActionResultDescription { get; set; } public string? UniqueId { get; set; } public object RawRequest { get; set; } public string RawResponse { get; set; } public dynamic? ResponseEntity { get; set; } public bool IsFriendlyTip { get; set; } = false; public string? FriendlyTip { get; set; } public bool IsRetry { get; set; } public string? TraceId { get; set; } }

public class PrepareRes : BaseRes
{
    public Payment Payment { get; set; }
    public string? RedirectUrl { get; set; }
}

public partial class Payment { public Guid PaymentId { get; set; } public string GatewayPayerId { get; set; } = null!; public short? LastStatus { get; set; } public DateTime CreateTime { get; set; } public string GatewayPaymentId { get; set; } = null!; public string Currency { get; set; } = null!; public decimal Price { get; set; } public string? Memo { get; set; } public string GatewayPayerName { get; set; } = null!; }

test data: {"InvoiceId":"03f38e68-792a-48c0-a8ee-1d7877b2f7c9","ActionResult":1,"ActionResultDescription":null,"UniqueId":null,"RawRequest":null,"RawResponse":null,"ResponseEntity":null,"StatusCode":null,"Errors":[],"IsRetry":false,"TraceId":null}

call code: source.Adapt(TypeAdapterConfig.GlobalSettings) ; or source.Adapt();

Error message:"Object reference not set to an instance of an object." StackTrack:" 在 Mapster.TypeAdapter.Adapt[TDestination](Object source, TypeAdapterConfig config)"

description: When deleting the TraceId attribute in the baseResponse class or changing the TraceId attribute in the source and target classes from string? to string, the conversion works fine.

ljf88 commented 1 month ago

Supplementary Description: If you change the type of TraceId to e.g. int?, long?, the conversion is also normal