Skip to content

Added switch to disable the LocalDate/LocalTime/Date format check #8408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/HotChocolate/Core/src/Types/Types/Scalars/DateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,32 @@ namespace HotChocolate.Types;
public class DateType : ScalarType<DateOnly, StringValueNode>
{
private const string DateFormat = "yyyy-MM-dd";
private readonly bool _enforceSpecFormat;

/// <summary>
/// Initializes a new instance of the <see cref="DateType"/> class.
/// </summary>
public DateType(
string name,
string? description = null,
BindingBehavior bind = BindingBehavior.Explicit)
BindingBehavior bind = BindingBehavior.Explicit,
bool disableFormatCheck = false)
: base(name, bind)
{
Description = description;
_enforceSpecFormat = !disableFormatCheck;
}

/// <summary>
/// Initializes a new instance of the <see cref="DateType"/> class.
/// </summary>
public DateType(bool disableFormatCheck)
: this(
ScalarNames.Date,
TypeResources.DateType_Description,
BindingBehavior.Implicit,
disableFormatCheck: disableFormatCheck)
{
}

/// <summary>
Expand Down Expand Up @@ -110,14 +125,27 @@ public override bool TryDeserialize(object? resultValue, out object? runtimeValu
private static string Serialize(IFormattable value) =>
value.ToString(DateFormat, CultureInfo.InvariantCulture);

private static bool TryDeserializeFromString(
private bool TryDeserializeFromString(
string? serialized,
[NotNullWhen(true)] out DateOnly? value)
{
if (DateOnly.TryParseExact(
serialized,
DateFormat,
out var date))
if (_enforceSpecFormat)
{
if (DateOnly.TryParseExact(
serialized,
DateFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var date))
{
value = date;
return true;
}
}
else if (DateOnly.TryParse(
serialized,
CultureInfo.InvariantCulture,
out var date))
{
value = date;
return true;
Expand Down
33 changes: 29 additions & 4 deletions src/HotChocolate/Core/src/Types/Types/Scalars/LocalDateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@ namespace HotChocolate.Types;
public class LocalDateType : ScalarType<DateOnly, StringValueNode>
{
private const string LocalFormat = "yyyy-MM-dd";
private readonly bool _enforceSpecFormat;

/// <summary>
/// Initializes a new instance of the <see cref="LocalDateType"/> class.
/// </summary>
public LocalDateType(
string name,
string? description = null,
BindingBehavior bind = BindingBehavior.Explicit)
BindingBehavior bind = BindingBehavior.Explicit,
bool disableFormatCheck = false)
: base(name, bind)
{
Description = description;
_enforceSpecFormat = !disableFormatCheck;
}

/// <summary>
/// Initializes a new instance of the <see cref="LocalDateType"/> class.
/// </summary>
public LocalDateType(bool disableFormatCheck)
: this(
ScalarNames.LocalDate,
TypeResources.LocalDateType_Description,
BindingBehavior.Implicit,
disableFormatCheck: disableFormatCheck)
{
}

/// <summary>
Expand Down Expand Up @@ -122,17 +137,27 @@ private static string Serialize(IFormattable value)
return value.ToString(LocalFormat, CultureInfo.InvariantCulture);
}

private static bool TryDeserializeFromString(
private bool TryDeserializeFromString(
string? serialized,
[NotNullWhen(true)] out DateOnly? value)
{
if (serialized is not null
&& DateOnly.TryParseExact(
if (_enforceSpecFormat)
{
if (DateOnly.TryParseExact(
serialized,
LocalFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var date))
{
value = date;
return true;
}
}
else if (DateOnly.TryParse(
serialized,
CultureInfo.InvariantCulture,
out var date))
{
value = date;
return true;
Expand Down
33 changes: 29 additions & 4 deletions src/HotChocolate/Core/src/Types/Types/Scalars/LocalTimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,32 @@ namespace HotChocolate.Types;
public class LocalTimeType : ScalarType<TimeOnly, StringValueNode>
{
private const string LocalFormat = "HH:mm:ss";
private readonly bool _enforceSpecFormat;

/// <summary>
/// Initializes a new instance of the <see cref="LocalTimeType"/> class.
/// </summary>
public LocalTimeType(
string name,
string? description = null,
BindingBehavior bind = BindingBehavior.Explicit)
BindingBehavior bind = BindingBehavior.Explicit,
bool disableFormatCheck = false)
: base(name, bind)
{
Description = description;
_enforceSpecFormat = !disableFormatCheck;
}

/// <summary>
/// Initializes a new instance of the <see cref="LocalTimeType"/> class.
/// </summary>
public LocalTimeType(bool disableFormatCheck)
: this(
ScalarNames.LocalTime,
TypeResources.LocalTimeType_Description,
BindingBehavior.Implicit,
disableFormatCheck: disableFormatCheck)
{
}

/// <summary>
Expand Down Expand Up @@ -121,17 +136,27 @@ private static string Serialize(IFormattable value)
return value.ToString(LocalFormat, CultureInfo.InvariantCulture);
}

private static bool TryDeserializeFromString(
private bool TryDeserializeFromString(
string? serialized,
[NotNullWhen(true)] out TimeOnly? value)
{
if (serialized is not null
&& TimeOnly.TryParseExact(
if (_enforceSpecFormat)
{
if (TimeOnly.TryParseExact(
serialized,
LocalFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var time))
{
value = time;
return true;
}
}
else if (TimeOnly.TryParse(
serialized,
CultureInfo.InvariantCulture,
out var time))
{
value = time;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,20 @@ public async Task DateOnly_As_ReturnValue()
.MatchSnapshotAsync();
}

[Fact]
public void DateType_Relaxed_Format_Check()
{
// arrange
const string s = "2011-08-30T08:46:14.116";

// act
var dateType = new DateType(disableFormatCheck: true);
var result = dateType.Deserialize(s);

// assert
Assert.IsType<DateOnly>(result);
}

public class Query
{
[GraphQLType(typeof(DateType))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,20 @@ public async Task DateOnly_As_ReturnValue()
.MatchSnapshotAsync();
}

[Fact]
public void LocalDate_Relaxed_Format_Check()
{
// arrange
const string s = "2011-08-30T08:46:14.116";

// act
var localDateType = new LocalDateType(disableFormatCheck: true);
var result = localDateType.Deserialize(s);

// assert
Assert.IsType<DateOnly>(result);
}

public class Query
{
[GraphQLType(typeof(LocalDateType))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,20 @@ public async Task TimeOnly_As_ReturnValue()
.MatchSnapshotAsync();
}

[Fact]
public void LocalTime_Relaxed_Format_Check()
{
// arrange
const string s = "8:46 am";

// act
var localTimeType = new LocalTimeType(disableFormatCheck: true);
var result = localTimeType.Deserialize(s);

// assert
Assert.IsType<TimeOnly>(result);
}

public class Query
{
[GraphQLType(typeof(LocalTimeType))]
Expand Down
Loading