mysqljsonsymfonydoctrinetruncated

Symfony, Doctrine truncates string in Json filed before storing in database


I'm facing a weird bug when storign some Json data in my Database with Doctrine in a Symfony 4 application.

Some strings in the json data are truncated over 27 characters and [...] is added at the end, but not always !!

Here's an example of the data I got in my DB :

{
  "tests": {
    "test-1": {
      "label": "Test 1",
      "someData": null,
      "uid": "044e0907-82cc-4f53-a325-e62830e59523"
    },
    "test-2": {
      "label": "Test 2",
      "someData": null,
      "uid": "a204b0a7-0831-4fde-976c-f3a1b0e75655"
    },
    "test-3": {
      "label": "Test 3",
      "someData": null,
      "uid": "d8f457b1-67d6-4ff7-9378-6c0ce5d9de0a"
    },
    "test-4": {
      "label": "Test 4",
      "someData": null,
      "uid": "5ddbd2eb-142c-4fbb-a4bc-d6 [...]" // Here is the bug !!!
    },
    "test-5": {
      "label": "Test 5",
      "someData": null,
      "uid": "e2ee7a1a-e0ae-4f1d-8806-967d94ddb790"
    }
  }
}

I spent time to debug to find where it could come, and before I flush my entity, the data of the property is ok, but after the flush, sometime, some of the uids (that are longer than 27 characters) are truncated.

$myEntity->setField($field);
$challenge->getField(); // Here the data is OK
$this->doctrine->getManagerForClass(MyEntity::class)->flush();
$challenge->getField(); // Here the data is truncated sometimes

Any idea where this bug could come from?

Doctrine ? Database (I use MySQL) ?

Thanks!


Solution

  • After some digging I finally found the bug, and it was my code's fault ^^

    To explain, before the flush, I made some transform to my array data, using a foreach loop with the value as reference.

    So the data array passed to the flush function kep the last item's value as reference. So, when the DbalLogger enters in action to log the query, it has the normalizeParams() function that shorten too long strings. And as some param value was passed by reference, it was shorten as well before being stored in DB!

    Conclusion: beware of passing reference in a foreach loop ;)