8000 [Serializer] Add support for auto generated custom normalizers by Nyholm · Pull Request #52905 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add support for auto generated custom normalizers #52905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only compare output on nikic/php-parser: 5
  • Loading branch information
Nyholm committed Jan 15, 2024
commit 9ee662b0894fd9ecc1b1d96b88b364c9273974e4
19 changes: 9 additions & 10 deletions src/Symfony/Component/Serializer/Builder/NormalizerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Builder\Namespace_;
use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
use Symfony\Component\Serializer\Exception\DenormalizingUnionFailedException;
Expand Down Expand Up @@ -301,7 +300,7 @@ private function addRequiredMethods(ClassDefinition $definition, Namespace_ $nam
->addParam($this->factory->param('format')->setType('?string'))
->setReturnType('array')
->addStmt(new Node\Stmt\Return_(new Node\Expr\Array_([
new Node\ArrayItem(
new Node\Expr\ArrayItem(
new Node\Expr\ConstFetch(new Node\Name('true')),
new Node\Expr\ClassConstFetch(new Node\Name($definition->getSourceClassName()), 'class')
),
Expand Down Expand Up @@ -370,7 +369,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
if (\is_object($defaultValue)) {
// public function __construct($foo = new \stdClass());
// There is no support for parameters to the object.
$defaultValue = new Expr\New_(new Node\Name\FullyQualified($defaultValue::class));
$defaultValue = new Node\Expr\New_(new Node\Name\FullyQualified($defaultValue::class));
} else {
$defaultValue = $this->factory->val($defaultValue);
}
Expand Down Expand Up @@ -404,7 +403,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified($targetClasses[0]), 'class')),
new Node\Arg(new Node\Expr\Variable('format')),
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Expr\ConstFetch(new Node\Name($canBeIterable ? 'true' : 'false'))),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name($canBeIterable ? 'true' : 'false'))),
]
)
)
Expand All @@ -413,7 +412,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
}

if ($propertyDefinition->hasConstructorDefaultValue()) {
$variableOutput = [new Node\Stmt\If_(new Expr\BooleanNot(
$variableOutput = [new Node\Stmt\If_(new Node\Expr\BooleanNot(
$this->factory->funcCall('array_key_exists', [
new Node\Arg(new Node\Scalar\String_($propertyDefinition->getNormalizedName())),
new Node\Arg(new Node\Expr\Variable('data')),
Expand Down Expand Up @@ -477,7 +476,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified($targetClasses[0]), 'class')),
new Node\Arg(new Node\Expr\Variable('format')),
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Expr\ConstFetch(new Node\Name($propertyDefinition->isCollection() ? 'true' : 'false'))),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name($propertyDefinition->isCollection() ? 'true' : 'false'))),
]
)
)
Expand Down Expand Up @@ -553,7 +552,7 @@ private function addNormailizeMethod(ClassDefinition $definition, Namespace_ $na
]);
}

$bodyArrayItems[] = new Node\ArrayItem($accessor, new Node\Scalar\String_($propertyDefinition->getNormalizedName()));
$bodyArrayItems[] = new Node\Expr\ArrayItem($accessor, new Node\Scalar\String_($propertyDefinition->getNormalizedName()));
}

// public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null;
Expand All @@ -577,11 +576,11 @@ private function addNormailizeMethod(ClassDefinition $definition, Namespace_ $na
*
* @return Node\Stmt[]
*/
private function generateCodeToDeserializeMultiplePossibleClasses(array $targetClasses, bool $canBeIterable, string $tempVariableName, Expr $variable, string $keyName, string $classNs): array
private function generateCodeToDeserializeMultiplePossibleClasses(array $targetClasses, bool $canBeIterable, string $tempVariableName, Node\Expr $variable, string $keyName, string $classNs): array
{
$arrayItems = [];
foreach ($targetClasses as $class) {
$arrayItems[] = new Node\ArrayItem(new Expr\ClassConstFetch(new Node\Name\FullyQualified($class), 'class'));
$arrayItems[] = new Node\Expr\ArrayItem(new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified($class), 'class'));
}

return [
Expand Down Expand Up @@ -616,7 +615,7 @@ private function generateCodeToDeserializeMultiplePossibleClasses(array $targetC
new Node\Arg(new Node\Expr\Variable('class')),
new Node\Arg(new Node\Expr\Variable('format')),
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Expr\ConstFetch(new Node\Name($canBeIterable ? 'true' : 'false'))),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name($canBeIterable ? 'true' : 'false'))),
]
)
)
Expand Down
66A4
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Serializer\Tests\Builder;

use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Builder\DefinitionExtractor;
use Symfony\Component\Serializer\Builder\NormalizerBuilder;
Expand All @@ -20,13 +21,17 @@ class NormalizerBuilderFixtureTest extends TestCase
private static NormalizerBuilder $builder;
private static DefinitionExtractor $definitionExtractor;
private static string $outputDir;
private static bool $compareOutput;

public static function setUpBeforeClass(): void
{
self::$definitionExtractor = FixtureHelper::getDefinitionExtractor();
self::$outputDir = \dirname(__DIR__).'/_output/SerializerBuilderFixtureTest';
self::$builder = new NormalizerBuilder();

// Only compare on nikic/php-parser: ^5.0
self::$compareOutput = method_exists(ParserFactory::class, 'createForVersion');

parent::setUpBeforeClass();
}

Expand All @@ -44,7 +49,10 @@ public function testBuildFixtures(string $inputClass, string $expectedOutputFile
$result = self::$builder->build($def, self::$outputDir);
$result->loadClass();
$this->assertTrue(class_exists($result->classNs));
$this->assertFileEquals($expectedOutputFile, $result->filePath);

if (self::$compareOutput) {
$this->assertFileEquals($expectedOutputFile, $result->filePath);
}
}

public static function fixtureClassGenerator(): iterable
Expand Down
0