You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
today I faced a weird issue.
Created configs for DateTime/DateTimeOffset/DateOnly mappins aren't used. This example should work or do I miss something?
public class Foo
{
public DateTimeOffset DateTimeOffset { get; set; }
public DateOnly Date { get; set; }
public DateTime DateTime { get; set; }
}
TypeAdapterConfig<DateTimeOffset, DateOnly>.NewConfig()
.MapWith(s => DateOnly.FromDateTime(s.UtcDateTime.Date))
.AfterMapping(s =>
{
Console.WriteLine("From DateTimeOffset to DateOnly");
});
TypeAdapterConfig<DateOnly, DateTimeOffset>.NewConfig()
.MapWith(s => new DateTimeOffset(s.ToDateTime(TimeOnly.FromTimeSpan(TimeSpan.Zero)), TimeSpan.Zero))
.AfterMapping(s =>
{
Console.WriteLine("From DateOnly to DateTimeOffset");
});
TypeAdapterConfig<DateTimeOffset, DateTime>.NewConfig()
.MapWith(s => s.UtcDateTime)
.AfterMapping(s =>
{
Console.WriteLine("From DateTimeOffset to DateTime");
});
TypeAdapterConfig<DateTime, DateTimeOffset>.NewConfig()
.MapWith(s => new DateTimeOffset(s, TimeSpan.Zero))
.AfterMapping(s =>
{
Console.WriteLine("From DateTime to DateTimeOffset");
});
var foo = new Foo();
foo.DateTimeOffset = DateTimeOffset.UtcNow;
foo.DateTime = DateTime.UtcNow;
Console.WriteLine(foo.DateTimeOffset.Adapt<DateTime>());
Console.WriteLine(foo.DateTime.Adapt<DateTimeOffset>());
TypeAdapterConfig<DateTimeOffset, DateOnly>.NewConfig()
.MapWith(s => DateOnly.FromDateTime(s.UtcDateTime.Date.AddDays(1)))
.AfterMapping(s =>
{
Console.WriteLine("From DateTimeOffset to DateOnly");
});
TypeAdapterConfig<DateOnly, DateTimeOffset>.NewConfig()
.MapWith(s => new DateTimeOffset(s.ToDateTime(TimeOnly.FromTimeSpan(TimeSpan.Zero)), TimeSpan.Zero).AddDays(1))
.AfterMapping(s =>
{
Console.WriteLine("From DateOnly to DateTimeOffset");
});
TypeAdapterConfig<DateTimeOffset, DateTime>.NewConfig()
.MapWith(s => s.UtcDateTime.AddDays(1))
.AfterMapping(s =>
{
Console.WriteLine("From DateTimeOffset to DateTime");
});
TypeAdapterConfig<DateTime, DateTimeOffset>.NewConfig()
.MapWith(s => new DateTimeOffset(s, TimeSpan.Zero).AddDays(1))
.AfterMapping(s =>
{
Console.WriteLine("From DateTime to DateTimeOffset");
});
The output with this modified code is:
4/30/2025 4:33:16 PM
4/30/2025 4:33:16 PM +00:00
which shows the Adapt is working.
What outputs are you expecting for each of your conversions? If you apply your conversion directly without using Mapster, do you get different results?
Using your 2 output examples and applying the conversion directly:
@stagep sorry, seems like it was too early for me yesterday.
You are totally right the mapping is working properly.
Initially I wanted to do something in AfterMapping but it's never invoked so I thought the mapping isn't used entirely. Should the AfterMapping work is this working as designed?
Activity
stagep commentedon Apr 29, 2025
The output with your code:
I added the following to every config
so now we have
The output with this modified code is:
which shows the Adapt is working.
What outputs are you expecting for each of your conversions? If you apply your conversion directly without using Mapster, do you get different results?
Using your 2 output examples and applying the conversion directly:
The output is
The same output (except the time has changed of course)
lucacivale commentedon Apr 30, 2025
@stagep sorry, seems like it was too early for me yesterday.
You are totally right the mapping is working properly.
Initially I wanted to do something in
AfterMappingbut it's never invoked so I thought the mapping isn't used entirely. Should theAfterMappingwork is this working as designed?DocSvartz commentedon Apr 30, 2025
@stagep @lucacivale
What version of Mapster are you using?
In 7.4.0 MapWith should not work with DateTime (Mapster primitive).MapWith by default does not merge settings with AfterMapping
This is activated separately
.MapWith(s => DateOnly.FromDateTime(s.UtcDateTime.Date), applySettings: true)
Warning:
In pre-releases
mapping to DateTime using MapWith and
applySettings: trueLeads to an infinite loop !!!
( Update: it fixed in new PR)
lucacivale commentedon Apr 30, 2025
@DocSvartz I'm using 7.4.0 and it's working without
applySettigns:Trueexcept theAfterMappingDocSvartz commentedon Apr 30, 2025
@lucacivale I'm probably already confusing something :). I dealt with this issue almost 2 years ago.
applySettigns:Trueactivate AfterMapping in 7.4.0 ?lucacivale commentedon May 2, 2025
Yes it works with
applySettigns:True@DocSvartz @stagep Thanks a lot!