Skip to content

Fix double-serialization of binary UUIDs in BelongsToMany relationships#38

Merged
kchung merged 9 commits intoWeebly:masterfrom
tlsqr:fix-double-serialization
Jan 22, 2026
Merged

Fix double-serialization of binary UUIDs in BelongsToMany relationships#38
kchung merged 9 commits intoWeebly:masterfrom
tlsqr:fix-double-serialization

Conversation

@tlsqr
Copy link
Contributor

@tlsqr tlsqr commented Jan 16, 2026

Summary

  • Fix "Incorrect UUID version" error when using BelongsToMany pivot operations with binary UUID keys
  • Add binary detection check to prevent double-serialization of already-serialized values

Problem

When using BelongsToMany relationships with binary UUID primary keys (via mutators), pivot operations like updateExistingPivot(), sync(), attach(), and detach() could fail with:

Ramsey\Uuid\Exception\InvalidArgumentException: Incorrect UUID version

This occurs because parseId() and parseIds() attempt to serialize values that are already in binary format. The existing cache check (in_array($value, $this->mutated_values)) only catches values serialized within the same instance, not binary values from other sources.

Root Cause

This bug was introduced with the upgrade to ramsey/uuid 4.x (Laravel 7+). Version 4.x is stricter about UUID validation and throws an exception when attempting to parse binary data as a UUID string.

Solution

Added a ctype_print() check before serialization to detect already-binary values:

  • UUID strings contain only printable characters (hex + hyphens) → ctype_print() returns true
  • Binary UUIDs (16 bytes) contain non-printable bytes → ctype_print() returns false

@tlsqr tlsqr requested a review from kchung January 19, 2026 14:37
@tlsqr tlsqr force-pushed the fix-double-serialization branch from 8755127 to 161bdd9 Compare January 21, 2026 20:43
@kchung kchung merged commit 75e9f83 into Weebly:master Jan 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants