Skip to content

Commit d05dc2f

Browse files
Fix relaxed-format Date/LocalDate parsing for UTC-suffixed values (#9854)
1 parent dee2704 commit d05dc2f

4 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/HotChocolate/Core/src/Types/Types/Scalars/DateType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ private bool TryParseStringValue(
114114
value = date;
115115
return true;
116116
}
117+
else if (DateTimeOffset.TryParse(
118+
serialized,
119+
CultureInfo.InvariantCulture,
120+
DateTimeStyles.None,
121+
out var dateTimeOffset))
122+
{
123+
value = DateOnly.FromDateTime(dateTimeOffset.UtcDateTime);
124+
return true;
125+
}
117126

118127
value = null;
119128
return false;

src/HotChocolate/Core/src/Types/Types/Scalars/LocalDateType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ private bool TryParseStringValue(string serialized, out DateOnly value)
113113
value = date;
114114
return true;
115115
}
116+
else if (DateTimeOffset.TryParse(
117+
serialized,
118+
CultureInfo.InvariantCulture,
119+
DateTimeStyles.None,
120+
out var dateTimeOffset))
121+
{
122+
value = DateOnly.FromDateTime(dateTimeOffset.DateTime);
123+
return true;
124+
}
116125

117126
value = default;
118127
return false;

src/HotChocolate/Core/test/Types.Tests/Types/Scalars/DateTypeTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,21 @@ public void DateType_Relaxed_Format_Check()
278278
Assert.IsType<DateOnly>(result);
279279
}
280280

281+
[Fact]
282+
public void DateType_Relaxed_Format_Check_With_Utc_Z_Suffix()
283+
{
284+
// arrange
285+
const string s = "2020-12-12T00:00:00.000Z";
286+
287+
// act
288+
var type = new DateType(disableFormatCheck: true);
289+
var inputValue = JsonDocument.Parse($"\"{s}\"").RootElement;
290+
var result = type.CoerceInputValue(inputValue, null!);
291+
292+
// assert
293+
Assert.Equal(new DateOnly(2020, 12, 12), Assert.IsType<DateOnly>(result));
294+
}
295+
281296
public class Query
282297
{
283298
[GraphQLType(typeof(DateType))]

src/HotChocolate/Core/test/Types.Tests/Types/Scalars/LocalDateTypeTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ public void CoerceInputValue()
106106
Assert.Equal(expectedDate, runtimeValue);
107107
}
108108

109+
[Fact]
110+
public void CoerceInputValue_Relaxed_Format_With_Time_Component()
111+
{
112+
// arrange
113+
const string s = "2011-08-30T08:46:14.116";
114+
115+
// act
116+
var type = new LocalDateType(disableFormatCheck: true);
117+
var inputValue = JsonDocument.Parse($"\"{s}\"").RootElement;
118+
var result = type.CoerceInputValue(inputValue, null!);
119+
120+
// assert
121+
Assert.Equal(new DateOnly(2011, 8, 30), Assert.IsType<DateOnly>(result));
122+
}
123+
124+
[Fact]
125+
public void CoerceInputValue_Relaxed_Format_With_Utc_Z_Suffix()
126+
{
127+
// arrange
128+
const string s = "2020-12-12T00:00:00.000Z";
129+
130+
// act
131+
var type = new LocalDateType(disableFormatCheck: true);
132+
var inputValue = JsonDocument.Parse($"\"{s}\"").RootElement;
133+
var result = type.CoerceInputValue(inputValue, null!);
134+
135+
// assert
136+
Assert.Equal(new DateOnly(2020, 12, 12), Assert.IsType<DateOnly>(result));
137+
}
138+
109139
[Fact]
110140
public void CoerceInputValue_Invalid_Format()
111141
{

0 commit comments

Comments
 (0)