From dfcf3eb7fe48fdbe8967efc0b60b8d30226f928b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Chav=C3=A9e?= Date: Mon, 20 Oct 2025 10:46:05 +0200 Subject: [PATCH 1/3] Fix returning "" when serializing empty strings --- src/Serializer/AbstractSerializer.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Serializer/AbstractSerializer.php b/src/Serializer/AbstractSerializer.php index 42946bf8d..2e19d09cc 100644 --- a/src/Serializer/AbstractSerializer.php +++ b/src/Serializer/AbstractSerializer.php @@ -216,6 +216,10 @@ protected function serializeObject($object, int $_depth = 0, array $hashes = []) */ protected function serializeString(string $value): string { + if ($value === '') { + return ''; + } + // we always guarantee this is coerced, even if we can't detect encoding if ($currentEncoding = mb_detect_encoding($value, $this->mbDetectOrder)) { $encoded = mb_convert_encoding($value, 'UTF-8', $currentEncoding) ?: ''; From d82d63b1e2fe355458e41e19cfa91f031f1e557c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Chav=C3=A9e?= Date: Mon, 20 Oct 2025 11:03:07 +0200 Subject: [PATCH 2/3] Remove spaces on empty line --- src/Serializer/AbstractSerializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serializer/AbstractSerializer.php b/src/Serializer/AbstractSerializer.php index 2e19d09cc..9e6312325 100644 --- a/src/Serializer/AbstractSerializer.php +++ b/src/Serializer/AbstractSerializer.php @@ -219,7 +219,7 @@ protected function serializeString(string $value): string if ($value === '') { return ''; } - + // we always guarantee this is coerced, even if we can't detect encoding if ($currentEncoding = mb_detect_encoding($value, $this->mbDetectOrder)) { $encoded = mb_convert_encoding($value, 'UTF-8', $currentEncoding) ?: ''; From 0877666bbab4e1f43dd91b95523008dc7adfb6d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Chav=C3=A9e?= Date: Mon, 20 Oct 2025 11:03:49 +0200 Subject: [PATCH 3/3] Add unit test to cover the change --- tests/Serializer/SerializerTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Serializer/SerializerTest.php b/tests/Serializer/SerializerTest.php index d402f210f..3f0662027 100644 --- a/tests/Serializer/SerializerTest.php +++ b/tests/Serializer/SerializerTest.php @@ -225,6 +225,15 @@ public function testClippingUTF8Characters(): void $this->assertSame(\JSON_ERROR_NONE, json_last_error()); } + public function testEmptyString(): void + { + $serializer = $this->createSerializer(); + + $result = $this->invokeSerialization($serializer, ''); + + $this->assertSame('', $result); + } + /** * @return Serializer */