Cysharp / Ulid

Fast .NET C# Implementation of ULID for .NET and Unity.
MIT License
1.33k stars 57 forks source link

Ulid.Parse cannot round trip with the guid string representation. #19

Closed akrock closed 3 years ago

akrock commented 3 years ago

Currently the implementation of Ulid.Parse will not parse a Ulid that is in GUID format back into the correct Ulid.

Example:

var ulidX = Ulid.NewUlid();
var guidX = ulidX.ToGuid();
var ulidXrt = Ulid.Parse(guidX.ToString());

var xeq = ulidX == ulidXrt;

var guidY = Guid.NewGuid();
var ulidY = Ulid.Parse(guidY.ToString());
var guidYrt = ulidY.ToGuid();

var yeq = guidY == guidYrt;

With this code in both cases the equality checks on the round-tripped from GUID string versions will fail to be the same as the original Ulid (or GUID). I would consider this unexpected and potentially dangerous behavior for data integrity purposes.

I would suggest that the implementation of Ulid.Parse should detect that a GUID format (not ULID) has been passed and either:

akrock commented 3 years ago

I think I see where the bug is. Will write some test cases and submit a PR in a few minutes hopefully.