Skip to content

Commit 01f3016

Browse files
authored
Changed the runtime type for the Date scalar in Strawberry Shake (#8009)
1 parent abd160d commit 01f3016

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/StrawberryShake/Client/src/Core/Serialization/DateSerializer.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace StrawberryShake.Serialization;
66
/// <summary>
77
/// This serializer handles date scalars.
88
/// </summary>
9-
public class DateSerializer : ScalarSerializer<string, DateTime>
9+
public class DateSerializer : ScalarSerializer<string, DateOnly>
1010
{
1111
private const string _dateFormat = "yyyy-MM-dd";
1212

@@ -15,32 +15,31 @@ public DateSerializer(string typeName = BuiltInScalarNames.Date)
1515
{
1616
}
1717

18-
public override DateTime Parse(string serializedValue)
18+
public override DateOnly Parse(string serializedValue)
1919
{
2020
if (TryDeserializeFromString(serializedValue, out var date))
2121
{
2222
return date.Value;
2323
}
2424

25-
throw ThrowHelper.DateTimeSerializer_InvalidFormat(serializedValue);
25+
throw ThrowHelper.DateSerializer_InvalidFormat(serializedValue);
2626
}
2727

28-
protected override string Format(DateTime runtimeValue)
28+
protected override string Format(DateOnly runtimeValue)
2929
{
30-
return runtimeValue.Date.ToString(_dateFormat, CultureInfo.InvariantCulture);
30+
return runtimeValue.ToString(_dateFormat, CultureInfo.InvariantCulture);
3131
}
3232

3333
private static bool TryDeserializeFromString(
3434
string? serialized,
35-
[NotNullWhen(true)] out DateTime? value)
35+
[NotNullWhen(true)] out DateOnly? value)
3636
{
37-
if (DateTime.TryParse(
37+
if (DateOnly.TryParseExact(
3838
serialized,
39-
CultureInfo.InvariantCulture,
40-
DateTimeStyles.AssumeLocal,
41-
out var dateTime))
39+
_dateFormat,
40+
out var date))
4241
{
43-
value = dateTime.Date;
42+
value = date;
4443
return true;
4544
}
4645

src/StrawberryShake/Client/test/Core.Tests/Serialization/DateSerializerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public void Parse()
1616
var result = Serializer.Parse(value);
1717

1818
// assert
19-
Assert.Equal(2012, result.Date.Year);
20-
Assert.Equal(11, result.Date.Month);
21-
Assert.Equal(29, result.Date.Day);
19+
Assert.Equal(2012, result.Year);
20+
Assert.Equal(11, result.Month);
21+
Assert.Equal(29, result.Day);
2222
}
2323

2424
[Fact]
@@ -37,7 +37,7 @@ public void Format_Null()
3737
public void Format_Value()
3838
{
3939
// arrange
40-
var value = new DateTime(2012, 11, 29);
40+
var value = new DateOnly(2012, 11, 29);
4141

4242
// act
4343
var result = Serializer.Format(value);

src/StrawberryShake/CodeGeneration/src/CodeGeneration/Utilities/SchemaHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private static void AddDefaultScalarInfos(
245245
TryAddLeafType(leafTypes, "Uuid", TypeNames.Guid, TypeNames.String);
246246
TryAddLeafType(leafTypes, "Guid", TypeNames.Guid, TypeNames.String);
247247
TryAddLeafType(leafTypes, ScalarNames.DateTime, TypeNames.DateTimeOffset);
248-
TryAddLeafType(leafTypes, ScalarNames.Date, TypeNames.DateTime);
248+
TryAddLeafType(leafTypes, ScalarNames.Date, TypeNames.DateOnly);
249249
TryAddLeafType(leafTypes, ScalarNames.LocalDate, TypeNames.DateOnly);
250250
TryAddLeafType(leafTypes, ScalarNames.LocalDateTime, TypeNames.DateTime);
251251
TryAddLeafType(leafTypes, ScalarNames.LocalTime, TypeNames.TimeOnly);

0 commit comments

Comments
 (0)