mongodb-odm icon indicating copy to clipboard operation
mongodb-odm copied to clipboard

UUID generator doesn't work with binary UUID types

Open alcaeus opened this issue 5 years ago • 6 comments

Bug Report

Q A
BC Break no
Version 2.0.3

Summary

Given an identifier using strategy="uuid" and type="bin_uuid" or type="bin_uuid_rfc4122", an error occurs when trying to store data:

MongoDB\Driver\Exception\InvalidArgumentException : Expected UUID length to be 16 bytes, 32 given

Expected behavior

The UUID generator should be able to generate UUIDs for consumption with the binary BSON types for UUIDs.

alcaeus avatar Jan 10 '20 13:01 alcaeus

Hi, Any news regarding this issue? I see the same error in version 2.1.2

dmitry84 avatar Nov 26 '20 00:11 dmitry84

I had lost track of this issue. I wanted to revisit this yesterday, but with travis-ci no longer reliably running our builds I had to prioritise moving the build environment. I'll pick this up once CI is stable again, but if you want to take a shot at fixing this, please do.

alcaeus avatar Nov 27 '20 07:11 alcaeus

The problem is the representation of string. uuid is passed as "string of hex" and not "hex string", "ab" = 16 bits not 8 bits, so the value is 32 bytes long instead the 16 bytes long (128 bits) attended size. using this "dirty" code in convertToDatabaseValue in BinDataType.php solve the problem

$str = $value;
        if (strlen($value) == 32){
            $str = '';
            for($i=0;$i<strlen($value);$i+=2){
                $str.=chr(hexdec(substr($value,$i,2)));
            }
        }
        if (! $value instanceof Binary) {
            return new Binary($str, $this->binDataType);
        }

Romaxx avatar May 11 '21 12:05 Romaxx

As a general rule, you shouldn't make such changes in the type itself, but rather in the code that generates the UUID. I just haven't gotten around to making the change and probably won't have the time to do so in the foreseeable future. If you're interested in working on this, I'm happy to help out.

alcaeus avatar May 13 '21 09:05 alcaeus

Is there any progress on this issue?

MVA18 avatar Aug 26 '22 09:08 MVA18

Given the lack of updates here, I'd say "obviously not".

alcaeus avatar Aug 26 '22 09:08 alcaeus