generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 44
fix: cast record ID to string to prevent JS precision loss with snowflake IDs #92
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
Merged
+54
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Relaticle\Flowforge\Tests\Fixtures\Task; | ||
| use Relaticle\Flowforge\Tests\Fixtures\TestBoard; | ||
|
|
||
| /** | ||
| * Regression test for GitHub issue #88: | ||
| * Large integer IDs (snowflakes) lose precision when passed through @js() in Blade. | ||
| * JavaScript's Number type uses IEEE 754 doubles, which can only safely represent | ||
| * integers up to 2^53 - 1 (9007199254740991). Snowflake IDs exceed this. | ||
| * | ||
| * @see https://github.com/relaticle/flowforge/issues/88 | ||
| */ | ||
| describe('snowflake ID precision', function () { | ||
| test('formatBoardRecord casts record ID to string to prevent JS precision loss', function () { | ||
| $task = Task::factory()->todo()->withPosition('65535.0000000000')->create(); | ||
|
|
||
| $board = app(TestBoard::class)->getBoard(); | ||
| $formatted = $board->formatBoardRecord($task); | ||
|
|
||
| // The ID must be a string so @js() emits a JSON string ("123") not a number (123) | ||
| // This prevents JavaScript precision loss for large IDs like snowflakes | ||
| expect($formatted['id'])->toBeString(); | ||
| }); | ||
|
|
||
| test('card blade renders recordKey as string in wire:click for large IDs', function () { | ||
| $task = Task::factory()->todo()->withPosition('65535.0000000000')->create(); | ||
|
|
||
| // Simulate what @js() does: json_encode the record ID | ||
| // If ID is an integer, json_encode produces a number literal which JS truncates | ||
| $idAsInt = (int) $task->id; | ||
| $jsonFromInt = json_encode(['recordKey' => $idAsInt]); | ||
|
|
||
| // If ID is a string, json_encode produces a quoted string which JS preserves | ||
| $idAsString = (string) $task->id; | ||
| $jsonFromString = json_encode(['recordKey' => $idAsString]); | ||
|
|
||
ManukMinasyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // For a snowflake like 420533451316027392: | ||
| // json_encode(int) -> {"recordKey":420533451316027392} <- JS reads as 420533451316027400 (WRONG) | ||
| // json_encode(string) -> {"recordKey":"420533451316027392"} <- JS reads correctly | ||
| $snowflakeId = 420533451316027392; | ||
| $jsonSnowflakeInt = json_encode(['recordKey' => $snowflakeId]); | ||
| $jsonSnowflakeStr = json_encode(['recordKey' => (string) $snowflakeId]); | ||
|
|
||
| // The string version wraps in quotes, preserving exact value | ||
| expect($jsonSnowflakeStr)->toContain('"420533451316027392"'); | ||
|
|
||
| // The int version does NOT wrap in quotes -- JS will lose precision | ||
| expect($jsonSnowflakeInt)->not->toContain('"420533451316027392"'); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.