-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Cursor Invalid Format Error (#6882)
- Loading branch information
1 parent
9062a25
commit 8c26d3b
Showing
12 changed files
with
237 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/HotChocolate/Core/src/Types.CursorPagination/IndexCursor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Buffers.Text; | ||
using System.Text; | ||
|
||
namespace HotChocolate.Types.Pagination; | ||
|
||
internal static class IndexCursor | ||
{ | ||
private static readonly Encoding _utf8 = Encoding.UTF8; | ||
|
||
public static unsafe string Format(Span<byte> buffer) | ||
{ | ||
fixed (byte* bytePtr = buffer) | ||
{ | ||
return _utf8.GetString(bytePtr, buffer.Length); | ||
} | ||
} | ||
|
||
public static unsafe bool TryParse(string cursor, out int index) | ||
{ | ||
fixed (char* cPtr = cursor) | ||
{ | ||
var count = _utf8.GetByteCount(cPtr, cursor.Length); | ||
byte[]? rented = null; | ||
|
||
var buffer = count <= 128 | ||
? stackalloc byte[count] | ||
: rented = ArrayPool<byte>.Shared.Rent(count); | ||
|
||
try | ||
{ | ||
fixed (byte* bytePtr = buffer) | ||
{ | ||
_utf8.GetBytes(cPtr, cursor.Length, bytePtr, buffer.Length); | ||
} | ||
|
||
Base64.DecodeFromUtf8InPlace(buffer, out var written); | ||
if (Utf8Parser.TryParse(buffer.Slice(0, written), out index, out _)) | ||
{ | ||
return true; | ||
} | ||
|
||
index = -1; | ||
return false; | ||
} | ||
finally | ||
{ | ||
if (rented is not null) | ||
{ | ||
ArrayPool<byte>.Shared.Return(rented); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/HotChocolate/Core/src/Types.CursorPagination/Properties/CursorResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...pes.CursorPagination.Tests/__snapshots__/IntegrationTests.Invalid_After_Index_Cursor.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "The cursor specified in `after` has an invalid format.", | ||
"locations": [ | ||
{ | ||
"line": 2, | ||
"column": 3 | ||
} | ||
], | ||
"path": [ | ||
"letters" | ||
], | ||
"extensions": { | ||
"argument": "after", | ||
"cursor": "INVALID", | ||
"code": "HC0078" | ||
} | ||
} | ||
], | ||
"data": { | ||
"letters": null | ||
} | ||
} |
Oops, something went wrong.