Skip to content

Commit 997f92c

Browse files
committed
allow to ignore properties during serialization
1 parent 78fcd31 commit 997f92c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/Symfony/Component/SerDes/Hook/Serialize/PropertyHookInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface PropertyHookInterface
2121
/**
2222
* @param array<string, mixed> $context
2323
*
24-
* @return array{name?: string, type?: string, accessor?: string, context?: array<string, mixed>}
24+
* @return array{name?: string, type?: string, accessor?: string|null, context?: array<string, mixed>}
2525
*/
2626
public function __invoke(\ReflectionProperty $property, string $accessor, array $context): array;
2727
}

src/Symfony/Component/SerDes/Internal/Serialize/TemplateGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ private function objectNodes(Type $type, NodeInterface $accessor, array $context
249249
if (null !== $hook) {
250250
$hookResult = $hook($property, (new Compiler())->compile($propertyAccessor)->source(), $context);
251251

252+
if (\array_key_exists('accessor', $hookResult) && null === $hookResult['accessor']) {
253+
continue;
254+
}
255+
252256
$propertyName = $hookResult['name'] ?? $propertyName;
253257
$propertyType = $hookResult['type'] ?? $propertyType;
254258
$propertyAccessor = isset($hookResult['accessor']) ? new RawNode($hookResult['accessor']) : $propertyAccessor;

src/Symfony/Component/SerDes/Tests/Internal/Serialize/Json/SerializeGenerateTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,33 @@ public static function serializeGenerateDataProvider(): iterable
321321
],
322322
],
323323
];
324+
325+
yield [
326+
<<<PHP
327+
<?php
328+
329+
/**
330+
* @param Symfony\Component\SerDes\Tests\Fixtures\Dto\ClassicDummy \$data
331+
* @param resource \$resource
332+
*/
333+
return static function (mixed \$data, mixed \$resource, array \$context): void {
334+
\$object_0 = \$data;
335+
\\fwrite(\$resource, "{\"name\":");
336+
\\fwrite(\$resource, \json_encode(\$object_0->name, \$context["json_encode_flags"] ?? 0));
337+
\\fwrite(\$resource, "}");
338+
};
339+
340+
PHP, ClassicDummy::class, [
341+
'hooks' => [
342+
'serialize' => [
343+
sprintf('%s::$id', ClassicDummy::class) => static function (\ReflectionProperty $property, string $accessor, array $context): array {
344+
return [
345+
'accessor' => null,
346+
];
347+
},
348+
],
349+
],
350+
],
351+
];
324352
}
325353
}

0 commit comments

Comments
 (0)