Skip to content

Commit 26b0a12

Browse files
github-actions[bot]eiriktsarpalisericstj
authored
[release/6.0] Fix TimeSpan support in sourcegen (#62191)
* fix TimeSpan support in sourcegen * address feedback * Address feedback Co-authored-by: Eirik Tsarpalis <[email protected]> Co-authored-by: Eric StJohn <[email protected]>
1 parent 5af5538 commit 26b0a12

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private sealed class Parser
8080
private readonly Type _objectType;
8181
private readonly Type _stringType;
8282

83+
private readonly Type? _timeSpanType;
8384
private readonly Type? _dateTimeOffsetType;
8485
private readonly Type? _byteArrayType;
8586
private readonly Type? _guidType;
@@ -202,6 +203,7 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
202203

203204
_booleanType = _metadataLoadContext.Resolve(SpecialType.System_Boolean);
204205
_charType = _metadataLoadContext.Resolve(SpecialType.System_Char);
206+
_timeSpanType = _metadataLoadContext.Resolve(typeof(TimeSpan));
205207
_dateTimeType = _metadataLoadContext.Resolve(SpecialType.System_DateTime);
206208
_nullableOfTType = _metadataLoadContext.Resolve(SpecialType.System_Nullable_T);
207209
_objectType = _metadataLoadContext.Resolve(SpecialType.System_Object);
@@ -1509,6 +1511,7 @@ private void PopulateKnownTypes()
15091511
_knownTypes.Add(_stringType);
15101512

15111513
AddTypeIfNotNull(_knownTypes, _byteArrayType);
1514+
AddTypeIfNotNull(_knownTypes, _timeSpanType);
15121515
AddTypeIfNotNull(_knownTypes, _dateTimeOffsetType);
15131516
AddTypeIfNotNull(_knownTypes, _guidType);
15141517
AddTypeIfNotNull(_knownTypes, _uriType);

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ protected static ActiveOrUpcomingEvent CreateActiveOrUpcomingEvent()
399399
EndDate = DateTime.UtcNow.AddYears(1),
400400
Name = "Just a name",
401401
ImageUrl = "https://www.dotnetfoundation.org/theme/img/carousel/foundation-diagram-content.png",
402-
StartDate = DateTime.UtcNow
402+
StartDate = DateTime.UtcNow,
403+
Offset = TimeSpan.FromHours(2)
403404
};
404405
}
405406

@@ -413,6 +414,7 @@ protected static void VerifyActiveOrUpcomingEvent(ActiveOrUpcomingEvent expected
413414
Assert.Equal(expected.ImageUrl, obj.ImageUrl);
414415
Assert.Equal(expected.Name, obj.Name);
415416
Assert.Equal(expected.StartDate, obj.StartDate);
417+
Assert.Equal(expected.Offset, obj.Offset);
416418
}
417419

418420
protected static CampaignSummaryViewModel CreateCampaignSummaryViewModel()

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class ActiveOrUpcomingEvent
6060
public string Description { get; set; }
6161
public DateTimeOffset StartDate { get; set; }
6262
public DateTimeOffset EndDate { get; set; }
63+
public TimeSpan Offset { get; set; }
6364
}
6465

6566
public class CampaignSummaryViewModel

0 commit comments

Comments
 (0)