Skip to content

Commit 213ca99

Browse files
committedSep 24, 2021
Fixed issue with the UUID parser when using the Utf8Parser (#4264)
1 parent ced1a64 commit 213ca99

File tree

2 files changed

+141
-20
lines changed

2 files changed

+141
-20
lines changed
 

‎src/HotChocolate/Core/src/Types/Types/Scalars/UuidType.cs

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Buffers.Text;
3+
using System.Linq;
4+
using System.Text;
35
using HotChocolate.Language;
46
using HotChocolate.Properties;
57

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

8688
protected override bool IsInstanceOfType(StringValueNode valueSyntax)
8789
{
88-
if (Utf8Parser.TryParse(valueSyntax.AsSpan(), out Guid _, out _, _format[0]))
90+
if (_enforceFormat)
8991
{
90-
return true;
91-
}
92+
ReadOnlySpan<byte> value = valueSyntax.AsSpan();
9293

93-
if (!_enforceFormat && Guid.TryParse(valueSyntax.Value, out _))
94+
if (Utf8Parser.TryParse(value, out Guid _, out var consumed, _format[0]) &&
95+
consumed == value.Length)
96+
{
97+
return true;
98+
}
99+
}
100+
else if(Guid.TryParse(valueSyntax.Value, out _))
94101
{
95102
return true;
96103
}
@@ -100,12 +107,17 @@ protected override bool IsInstanceOfType(StringValueNode valueSyntax)
100107

101108
protected override Guid ParseLiteral(StringValueNode valueSyntax)
102109
{
103-
if (Utf8Parser.TryParse(valueSyntax.AsSpan(), out Guid g, out _, _format[0]))
110+
if (_enforceFormat)
104111
{
105-
return g;
106-
}
112+
ReadOnlySpan<byte> value = valueSyntax.AsSpan();
107113

108-
if (!_enforceFormat && Guid.TryParse(valueSyntax.Value, out g))
114+
if (Utf8Parser.TryParse(value, out Guid g, out var consumed, _format[0]) &&
115+
consumed == value.Length)
116+
{
117+
return g;
118+
}
119+
}
120+
else if(Guid.TryParse(valueSyntax.Value, out Guid g))
109121
{
110122
return g;
111123
}
@@ -168,10 +180,23 @@ public override bool TryDeserialize(object? resultValue, out object? runtimeValu
168180
return true;
169181
}
170182

171-
if (resultValue is string s && Guid.TryParse(s, out Guid guid))
183+
if (resultValue is string s)
172184
{
173-
runtimeValue = guid;
174-
return true;
185+
byte[] bytes = Encoding.UTF8.GetBytes(s);
186+
187+
if (_enforceFormat &&
188+
Utf8Parser.TryParse(bytes, out Guid guid, out var consumed, _format[0]) &&
189+
consumed == bytes.Length)
190+
{
191+
runtimeValue = guid;
192+
return true;
193+
}
194+
195+
if (!_enforceFormat && Guid.TryParse(s, out guid))
196+
{
197+
runtimeValue = guid;
198+
return true;
199+
}
175200
}
176201

177202
if (resultValue is Guid)

‎src/HotChocolate/Core/test/Types.Tests/Types/Scalars/UuidTypeTests.cs

+105-9
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void Serialize_Null()
8686
var uuidType = new UuidType();
8787

8888
// act
89-
object? serializedValue = uuidType.Serialize(null);
89+
var serializedValue = uuidType.Serialize(null);
9090

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

115115
// act
116-
var success = uuidType.TryDeserialize(null, out object? o);
116+
var success = uuidType.TryDeserialize(null, out var o);
117117

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

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

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

145145
// act
146-
var success = uuidType.TryDeserialize(guid, out object? o);
146+
var success = uuidType.TryDeserialize(guid, out var o);
147147

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

221221
// act
222-
object? value = uuidType.ParseLiteral(literal);
222+
var value = uuidType.ParseLiteral(literal);
223223

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

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

376376
// assert
377-
#if NETCOREAPP2_1
377+
#if NETCOREAPP2_1
378378
Assert.Throws<ArgumentException>(Action).Message
379379
.MatchSnapshot(new SnapshotNameExtension("NETCOREAPP2_1"));
380-
#else
380+
#else
381381
Assert.Throws<ArgumentException>(Action).Message.MatchSnapshot();
382-
#endif
382+
#endif
383+
}
384+
385+
[InlineData(false)]
386+
[InlineData(true)]
387+
[Theory]
388+
public void Parse_Guid_String_With_Appended_String(bool enforceFormat)
389+
{
390+
// arrange
391+
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f-foobar");
392+
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);
393+
394+
// act
395+
void Fail() => uuidType.ParseLiteral(input);
396+
397+
// assert
398+
Assert.Throws<SerializationException>(Fail);
399+
}
400+
401+
[InlineData(false)]
402+
[InlineData(true)]
403+
[Theory]
404+
public void Parse_Guid_Valid_Input(bool enforceFormat)
405+
{
406+
// arrange
407+
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f");
408+
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);
409+
410+
// act
411+
var guid = (Guid)uuidType.ParseLiteral(input)!;
412+
413+
// assert
414+
Assert.Equal(input.Value, guid.ToString("D"));
415+
}
416+
417+
[InlineData(false)]
418+
[InlineData(true)]
419+
[Theory]
420+
public void Deserialize_Guid_String_With_Appended_String(bool enforceFormat)
421+
{
422+
// arrange
423+
var input = "fbdef721-93c5-4267-8f92-ca27b60aa51f-foobar";
424+
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);
425+
426+
// act
427+
void Fail() => uuidType.Deserialize(input);
428+
429+
// assert
430+
Assert.Throws<SerializationException>(Fail);
431+
}
432+
433+
[InlineData(false)]
434+
[InlineData(true)]
435+
[Theory]
436+
public void Deserialize_Guid_Valid_Format(bool enforceFormat)
437+
{
438+
// arrange
439+
var input = "fbdef721-93c5-4267-8f92-ca27b60aa51f";
440+
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);
441+
442+
// act
443+
var guid = (Guid)uuidType.Deserialize(input)!;
444+
445+
// assert
446+
Assert.Equal(input, guid.ToString("D"));
447+
}
448+
449+
[InlineData(false)]
450+
[InlineData(true)]
451+
[Theory]
452+
public void IsInstanceOf_Guid_String_With_Appended_String(bool enforceFormat)
453+
{
454+
// arrange
455+
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f-foobar");
456+
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);
457+
458+
// act
459+
var result = uuidType.IsInstanceOfType(input);
460+
461+
// assert
462+
Assert.False(result);
463+
}
464+
465+
[InlineData(false)]
466+
[InlineData(true)]
467+
[Theory]
468+
public void IsInstanceOf_Guid_Valid_Format(bool enforceFormat)
469+
{
470+
// arrange
471+
var input = new StringValueNode("fbdef721-93c5-4267-8f92-ca27b60aa51f");
472+
var uuidType = new UuidType(defaultFormat: 'D', enforceFormat: enforceFormat);
473+
474+
// act
475+
var result = uuidType.IsInstanceOfType(input);
476+
477+
// assert
478+
Assert.True(result);
383479
}
384480
}
385481
}

0 commit comments

Comments
 (0)
Please sign in to comment.