fix: preserve serialized object payloads

This commit is contained in:
Keith Solomon
2026-04-26 14:57:48 -05:00
parent 4cfc1036bb
commit d50917d827
2 changed files with 29 additions and 1 deletions
+22 -1
View File
@@ -32,7 +32,7 @@ final class MetadataUrlTransformer {
if ( $this->looksSerialized( $value ) ) {
$unserialized = $this->unserializeValue( $value );
if ( ! $unserialized['valid'] || is_object( $unserialized['value'] ) ) {
if ( ! $unserialized['valid'] || $this->containsObject( $unserialized['value'] ) ) {
return $value;
}
@@ -118,4 +118,25 @@ final class MetadataUrlTransformer {
return '' !== $trimmed && in_array( $trimmed[0], array( '{', '[' ), true );
}
/**
* @param mixed $value Value to inspect.
*/
private function containsObject( $value ): bool {
if ( is_object( $value ) ) {
return true;
}
if ( ! is_array( $value ) ) {
return false;
}
foreach ( $value as $item ) {
if ( $this->containsObject( $item ) ) {
return true;
}
}
return false;
}
}
@@ -100,6 +100,13 @@ class MetadataUrlTransformerTest extends TestCase {
self::assertSame( $value, $transformer->transformValue( $value, $this->mappings() ) );
}
public function test_it_leaves_serialized_payloads_with_nested_objects_unchanged(): void {
$transformer = new MetadataUrlTransformer( new UrlTransformer() );
$value = 'a:2:{s:3:"url";s:28:"https://example.test/inside";s:6:"object";O:8:"stdClass":0:{}}';
self::assertSame( $value, $transformer->transformValue( $value, $this->mappings() ) );
}
public function test_it_leaves_invalid_json_strings_unchanged(): void {
$transformer = new MetadataUrlTransformer( new UrlTransformer() );
$value = '{"url":"https://example.test/missing"';