Repository navigation
Unable to map nullable primitive types (string? to bool?) using custom MapWith delegate #992
Description
Activity
@vzalamea What Version of Mapster you using?
Reacted by Vince Zalameaworks in 10.0.8. broken in 10.0.9
in 10.0.9 Nullable can using not nullable Mapping settings. Try using this:
config.NewConfig<string, bool>().MapWith(src => Helper.ParseToNullableBool(src)); class Source { public string? Prop1 { get; set; } } class Destination { public bool? Prop1 { get; set; } }- linked a pull request that will close this issueQuick fix Custom converter supporting from nullable ValueType #993
on Jun 29, 2026 I'm seeing the same issue.
I have a global converter:
TypeAdapterConfig.GlobalSettings .ForType<DateTime?, long?>() .MapWith(src => src.ToSecondsTimestamp());
Source:
public DateTime? CreatedAt { get; set; }
Destination:
public long? CreatedAt { get; set; }
Calling:
var dto = entity.Adapt<TimeLogExtendRsRes>();
throws:
System.InvalidOperationException: Nullable object must have a value.Hi, @nghiaphamhn This is due to the addition of support for using mapping settings for ValueType when mapping its Nullable representations:
.Adapt<DateTime?, long?>(),.Adapt<DateTime, long?>(),.Adapt<DateTime?, long>()
will automatically use the settings forForType<DateTime, long>unless you specify a more specific equivalent:.ForType<DateTime?, long?>()or.ForType<DateTime, long?>()or.ForType<DateTime?, long>().This was the original idea, but it seems there were problems with the prioritization of settings.
Your case should work correctly in version 10.0.10 If you don't have a custom converter installed for
.ForType<DateTime, long>(), it will still have higher priority. It will be fixed in the next version.Thanks for the explanation, @DocSvartz
In my case, I explicitly configured converters for all nullable and non-nullable combinations:
GlobalConfig.ForType<DateTime, long>().MapWith(src => (long)src.ToSecondsTimestamp());
GlobalConfig.ForType<DateTime?, long>().MapWith(src => (long)src.ToSecondsTimestamp());
GlobalConfig.ForType<DateTime, long?>().MapWith(src => src.ToSecondsTimestamp());
GlobalConfig.ForType<DateTime?, long?>().MapWith(src => src.ToSecondsTimestamp());
GlobalConfig.ForType<long, DateTime>().MapWith(src => src.FromUnixTimeStamp());
GlobalConfig.ForType<long, DateTime?>().MapWith(src => src.FromUnixTimeStamp());
GlobalConfig.ForType<long?, DateTime>().MapWith(src => (DateTime)src.FromUnixTimeStamp());
GlobalConfig.ForType<long?, DateTime?>().MapWith(src => src.FromUnixTimeStamp());Just to clarify, I'm currently using Mapster 10.0.10, and the issue still occurs with my configuration.
The same configuration works correctly on 10.0.8. The issue only appears after upgrading to 10.0.9 and 10.0.10.@nghiaphamhn I think I understood what the problem is.
GlobalConfig.ForType<DateTime?, long>().MapWith(src => (long)src.ToSecondsTimestamp());it is valid only DateTime is not null
Otherwise, you will get the error you are talking about:System.InvalidOperationException: Nullable object must have a value.It's safer to use this:
src.ToSecondsTimestamp().GetValueOrDefault();I think this will also be fixed and will work as before.
- added a commit that references this issue
on Jul 15, 2026 Thanks @DocSvartz , this solution works correctly now:
src.ToSecondsTimestamp().GetValueOrDefault();
The
Nullable object must have a valueerror is no longer occurring.
What am I missing?
converting non-nullable string to non-nullable bool works fine