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
Generate new classes
  • Loading branch information
Nyholm committed Jan 15, 2024
commit d9be208252d62f7fcad9f48b4b2a8329abc40a87
101 changes: 52 additions & 49 deletions src/Symfony/Component/Serializer/Builder/NormalizerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,18 @@ private function generateNormalizeChildMethod(Namespace_ $namespace, Class_ $cla
new Node\Name('array_map'),
[
new Node\Arg(
new Node\Expr\Closure([
new Node\Expr\ArrowFunction([
'params' => [new Node\Param(new Node\Expr\Variable('item'))],
'stmts' => [
new Node\Stmt\Return_(
$this->factory->methodCall(
new Node\Expr\Variable('this'),
'normalizeChild',
[
new Node\Arg(new Node\Expr\Variable('item')),
new Node\Arg(new Node\Expr\Variable('format')),
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name('true'))),
]
)
),
],
'expr' => $this->factory->methodCall(
new Node\Expr\Variable('this'),
'normalizeChild',
[
new Node\Arg(new Node\Expr\Variable('item')),
new Node\Arg(new Node\Expr\Variable('format')),
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name('true'))),
]
)
])
),
new Node\Arg(new Node\Expr\Variable('object')),
Expand Down Expand Up @@ -251,23 +247,19 @@ private function generateDenormalizeChildMethod(Namespace_ $namespace, Class_ $c
new Node\Name('array_map'),
[
new Node\Arg(
new Node\Expr\Closure([
new Node\Expr\ArrowFunction([
'params' => [new Node\Param(new Node\Expr\Variable('item'))],
'stmts' => [
new Node\Stmt\Return_(
$this->factory->methodCall(
new Node\Expr\Variable('this'),
'denormalizeChild',
[
new Node\Arg(new Node\Expr\Variable('item')),
new Node\Arg(new Node\Expr\Variable('type')),
new Node\Arg(new Node\Expr\Variable('format')),
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name('true'))),
]
)
),
],
'expr' => $this->factory->methodCall(
new Node\Expr\Variable('this'),
'denormalizeChild',
[
new Node\Arg(new Node\Expr\Variable('item')),
new Node\Arg(new Node\Expr\Variable('type')),
new Node\Arg(new Node\Expr\Variable('format')),
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name('true'))),
]
)
])
),
new Node\Arg(new Node\Expr\Variable('data')),
Expand Down Expand Up @@ -376,14 +368,23 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
$targetClasses = $propertyDefinition->getNonPrimitiveTypes();
$canBeIterable = $propertyDefinition->isCollection();

$defaultValue = $propertyDefinition->getConstructorDefaultValue();
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(get_class($defaultValue)));
} else {
$defaultValue = $this->factory->val($defaultValue);
}

if ([] === $targetClasses && $propertyDefinition->hasConstructorDefaultValue()) {
$constructorArguments[] = new Node\Arg(new Node\Expr\BinaryOp\Coalesce(
$variable,
$this->factory->val($propertyDefinition->getConstructorDefaultValue())
$defaultValue
));
continue;
} elseif ([] === $targetClasses) {
$constructorArguments[] = new Node\Arg(new Node\Expr\Variable($variable));
$constructorArguments[] = new Node\Arg($variable);
continue;
}

Expand All @@ -402,7 +403,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
'denormalizeChild',
[
new Node\Arg($variable),
new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name($targetClasses[0]), 'class')),
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'))),
Expand All @@ -423,10 +424,10 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
'stmts' => [
new Node\Stmt\Expression(new Node\Expr\Assign(
new Node\Expr\Variable($tempVariableName),
$this->factory->val($propertyDefinition->getConstructorDefaultValue())
$defaultValue
)),
],
'else' => $variableOutput
'else' => new Node\Stmt\Else_($variableOutput)
]
)];
}
Expand Down Expand Up @@ -475,7 +476,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
'denormalizeChild',
[
new Node\Arg($variable),
new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name($targetClasses[0]), 'class')),
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'))),
Expand Down Expand Up @@ -519,7 +520,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
->addParam($this->factory->param('format')->setType('?string')->setDefault(null))
->addParam($this->factory->param('context')->setType('array')->setDefault([]))
->setReturnType('mixed')
->addStmts([])
->addStmts($body)
->addStmt(new Node\Stmt\Return_(new Node\Expr\Variable('output')))
);

Expand Down Expand Up @@ -566,7 +567,7 @@ private function addNormailizeMethod(ClassDefinition $definition, Namespace_ $na
->addParam($this->factory->param('format')->setType('string')->setDefault(null))
->addParam($this->factory->param('context')->setType('array')->setDefault([]))
->setReturnType('array|string|int|float|bool|\ArrayObject|null')
->setDocComment(sprintf('/*'.PHP_EOL.'* @param %s $object'.PHP_EOL.'*/', $definition->getSourceClassName()))
->setDocComment(sprintf('/**'.PHP_EOL.'* @param %s $object'.PHP_EOL.'*/', $definition->getSourceClassName()))
->addStmt(new Node\Stmt\Return_(new Node\Expr\Array_($bodyArrayItems))));

if ($needsChildNormalizer) {
Expand All @@ -584,7 +585,7 @@ private function generateCodeToDeserializeMultiplePossibleClasses(array $targetC
{
$arrayItems = [];
foreach ($targetClasses as $class) {
$arrayItems[] = new Node\ArrayItem(new Node\Scalar\String_($class));
$arrayItems[] = new Node\ArrayItem(new Expr\ClassConstFetch(new Node\Name\FullyQualified($class), 'class'));
}

return [
Expand Down Expand Up @@ -655,15 +656,17 @@ private function generateCodeToDeserializeMultiplePossibleClasses(array $targetC
new Node\Expr\BooleanNot(new Node\Expr\Variable($tempVariableName.'HasValue')),
[
'stmts' => [
new Node\Expr\Throw_(
new Node\Expr\New_(
new Node\Name\FullyQualified(DenormalizingUnionFailedException::class),
[
new Node\Arg(new Node\Scalar\String_('Failed to denormalize key "'.$keyName.'" of class "'.$classNs.'".')),
new Node\Arg(new Node\Expr\Variable('exceptions')),
]
)
),
new Node\Stmt\Expression(
new Node\Expr\Throw_(
new Node\Expr\New_(
new Node\Name('DenormalizingUnionFailedException'),
[
new Node\Arg(new Node\Scalar\String_('Failed to denormalize key "'.$keyName.'" of class "'.$classNs.'".')),
new Node\Arg(new Node\Expr\Variable('exceptions')),
]
)
),
)
],
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function includeIfExists(string $file): bool

echo \PHP_EOL;
$i = 0;
foreach (FixtureHelper::getFixturesAndResultFiles() as [$class => $outputFile]) {
foreach (FixtureHelper::getFixturesAndResultFiles() as $class => $outputFile) {
$definition = $definitionExtractor->getDefinition($class);
$result = $builder->build($definition, $outputDir);
$result->loadClass();
Expand Down
Loading
0