Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ChilliCream/graphql-platform
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 11.3.7
Choose a base ref
...
head repository: ChilliCream/graphql-platform
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 11.3.8
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Sep 24, 2021

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    Mic92 Jörg Thalheim
    Copy the full SHA
    213ca99 View commit details
Showing with 141 additions and 20 deletions.
  1. +36 −11 src/HotChocolate/Core/src/Types/Types/Scalars/UuidType.cs
  2. +105 −9 src/HotChocolate/Core/test/Types.Tests/Types/Scalars/UuidTypeTests.cs
47 changes: 36 additions & 11 deletions src/HotChocolate/Core/src/Types/Types/Scalars/UuidType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Buffers.Text;
using System.Linq;
using System.Text;
using HotChocolate.Language;
using HotChocolate.Properties;

@@ -85,12 +87,17 @@ public UuidType(

protected override bool IsInstanceOfType(StringValueNode valueSyntax)
{
if (Utf8Parser.TryParse(valueSyntax.AsSpan(), out Guid _, out _, _format[0]))
if (_enforceFormat)
{
return true;
}
ReadOnlySpan<byte> value = valueSyntax.AsSpan();

if (!_enforceFormat && Guid.TryParse(valueSyntax.Value, out _))
if (Utf8Parser.TryParse(value, out Guid _, out var consumed, _format[0]) &&
consumed == value.Length)
{
return true;
}
}
else if(Guid.TryParse(valueSyntax.Value, out _))
{
return true;
}
@@ -100,12 +107,17 @@ protected override bool IsInstanceOfType(StringValueNode valueSyntax)

protected override Guid ParseLiteral(StringValueNode valueSyntax)
{
if (Utf8Parser.TryParse(valueSyntax.AsSpan(), out Guid g, out _, _format[0]))
if (_enforceFormat)
{
return g;
}
ReadOnlySpan<byte> value = valueSyntax.AsSpan();

if (!_enforceFormat && Guid.TryParse(valueSyntax.Value, out g))
if (Utf8Parser.TryParse(value, out Guid g, out var consumed, _format[0]) &&
consumed == value.Length)
{
return g;
}
}
else if(Guid.TryParse(valueSyntax.Value, out Guid g))
{
return g;
}
@@ -168,10 +180,23 @@ public override bool TryDeserialize(object? resultValue, out object? runtimeValu
return true;
}

if (resultValue is string s && Guid.TryParse(s, out Guid guid))
if (resultValue is string s)
{
runtimeValue = guid;
return true;
byte[] bytes = Encoding.UTF8.GetBytes(s);

if (_enforceFormat &&
Utf8Parser.TryParse(bytes, out Guid guid, out var consumed, _format[0]) &&
consumed == bytes.Length)
{
runtimeValue = guid;
return true;
}

if (!_enforceFormat && Guid.TryParse(s, out guid))
{
runtimeValue = guid;
return true;
}
}

if (resultValue is Guid)
114 changes: 105 additions & 9 deletions src/HotChocolate/Core/test/Types.Tests/Types/Scalars/UuidTypeTests.cs
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ public void Serialize_Null()
var uuidType = new UuidType();

// act
object? serializedValue = uuidType.Serialize(null);
var serializedValue = uuidType.Serialize(null);

// assert
Assert.Null(serializedValue);
@@ -113,7 +113,7 @@ public void Deserialize_Null()
var uuidType = new UuidType();

// act
var success = uuidType.TryDeserialize(null, out object? o);
var success = uuidType.TryDeserialize(null, out var o);

// assert
Assert.True(success);
@@ -128,7 +128,7 @@ public void Deserialize_String()
var guid = Guid.NewGuid();

// act
var success = uuidType.TryDeserialize(guid.ToString("N"), out object? o);
var success = uuidType.TryDeserialize(guid.ToString("N"), out var o);

// assert
Assert.True(success);
@@ -143,7 +143,7 @@ public void Deserialize_Guid()
var guid = Guid.NewGuid();

// act
var success = uuidType.TryDeserialize(guid, out object? o);
var success = uuidType.TryDeserialize(guid, out var o);

// assert
Assert.True(success);
@@ -219,7 +219,7 @@ public void ParseLiteral_NullValueNode()
NullValueNode literal = NullValueNode.Default;

// act
object? value = uuidType.ParseLiteral(literal);
var value = uuidType.ParseLiteral(literal);

// assert
Assert.Null(value);
@@ -283,7 +283,7 @@ public void ParseValue_Int()
}

[Fact]
public void EnsureDateTypeKindIsCorret()
public void EnsureDateTypeKindIsCorrect()
{
// arrange
var type = new UuidType();
@@ -374,12 +374,108 @@ public void Specify_Invalid_Format()
void Action() => new UuidType(defaultFormat: 'z');

// assert
#if NETCOREAPP2_1
#if NETCOREAPP2_1
Assert.Throws<ArgumentException>(Action).Message
.MatchSnapshot(new SnapshotNameExtension("NETCOREAPP2_1"));
#else
#else
Assert.Throws<ArgumentException>(Action).Message.MatchSnapshot();
#endif
#endif
}

[InlineData(false)]
[InlineData(true)]
[Theory]
public void Parse_Guid_String_With_Appended_String(bool enforceFormat)
{
// arrange
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f-foobar");
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);

// act
void Fail() => uuidType.ParseLiteral(input);

// assert
Assert.Throws<SerializationException>(Fail);
}

[InlineData(false)]
[InlineData(true)]
[Theory]
public void Parse_Guid_Valid_Input(bool enforceFormat)
{
// arrange
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f");
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);

// act
var guid = (Guid)uuidType.ParseLiteral(input)!;

// assert
Assert.Equal(input.Value, guid.ToString("D"));
}

[InlineData(false)]
[InlineData(true)]
[Theory]
public void Deserialize_Guid_String_With_Appended_String(bool enforceFormat)
{
// arrange
var input = "fbdef721-93c5-4267-8f92-ca27b60aa51f-foobar";
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);

// act
void Fail() => uuidType.Deserialize(input);

// assert
Assert.Throws<SerializationException>(Fail);
}

[InlineData(false)]
[InlineData(true)]
[Theory]
public void Deserialize_Guid_Valid_Format(bool enforceFormat)
{
// arrange
var input = "fbdef721-93c5-4267-8f92-ca27b60aa51f";
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);

// act
var guid = (Guid)uuidType.Deserialize(input)!;

// assert
Assert.Equal(input, guid.ToString("D"));
}

[InlineData(false)]
[InlineData(true)]
[Theory]
public void IsInstanceOf_Guid_String_With_Appended_String(bool enforceFormat)
{
// arrange
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f-foobar");
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);

// act
var result = uuidType.IsInstanceOfType(input);

// assert
Assert.False(result);
}

[InlineData(false)]
[InlineData(true)]
[Theory]
public void IsInstanceOf_Guid_Valid_Format(bool enforceFormat)
{
// arrange
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f");
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);

// act
var result = uuidType.IsInstanceOfType(input);

// assert
Assert.True(result);
}
}
}