From c3fd7db43101a58450aa9c2ed2f93ba67bbeea9b Mon Sep 17 00:00:00 2001 From: tecbird <16688055+tecbird@users.noreply.github.com> Date: Fri, 17 Jan 2025 14:47:35 +0100 Subject: [PATCH 1/2] Do not skip "nullable" properties in normalize we need "nullable" values in the result of the normalizer call, because we want to explicit write "null" value via api call --- Generator/Normalizer/NormalizerGenerator.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Generator/Normalizer/NormalizerGenerator.php b/Generator/Normalizer/NormalizerGenerator.php index 1e05cd2..8549b7b 100644 --- a/Generator/Normalizer/NormalizerGenerator.php +++ b/Generator/Normalizer/NormalizerGenerator.php @@ -124,13 +124,20 @@ protected function createNormalizeMethod(string $modelFqdn, Context $context, Cl } if (!$property->isRequired()) { - $statements[] = new Stmt\If_( - new Expr\BinaryOp\BooleanAnd( + if ($property->isNullable()) { + $statements[] = new Stmt\If_( new Expr\MethodCall($objectVariable, 'isInitialized', [new Arg(new Scalar\String_($property->getPhpName()))]), - new Expr\BinaryOp\NotIdentical(new Expr\ConstFetch(new Name('null')), $propertyVar) - ), - ['stmts' => $normalizationStatements] - ); + ['stmts' => $normalizationStatements] + ); + } else { + $statements[] = new Stmt\If_( + new Expr\BinaryOp\BooleanAnd( + new Expr\MethodCall($objectVariable, 'isInitialized', [new Arg(new Scalar\String_($property->getPhpName()))]), + new Expr\BinaryOp\NotIdentical(new Expr\ConstFetch(new Name('null')), $propertyVar) + ), + ['stmts' => $normalizationStatements] + ); + } } else { $statements[] = new Stmt\If_( new Expr\BinaryOp\NotIdentical(new Expr\ConstFetch(new Name('null')), $propertyVar), From 77b7e99f2bcb65c1c5f865c85210b880bb87c289 Mon Sep 17 00:00:00 2001 From: tecbird <16688055+tecbird@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:35:25 +0100 Subject: [PATCH 2/2] Fix condition for "nullable" properties --- Guesser/Guess/CheckNullableTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guesser/Guess/CheckNullableTrait.php b/Guesser/Guess/CheckNullableTrait.php index 067de0c..d72d8ac 100644 --- a/Guesser/Guess/CheckNullableTrait.php +++ b/Guesser/Guess/CheckNullableTrait.php @@ -15,7 +15,7 @@ public function isNullable($schema): bool return $schema->offsetExists('x-nullable') && \is_bool($schema->offsetGet('x-nullable')) && $schema->offsetGet('x-nullable'); } if (\get_class($schema) === 'Jane\\Component\\OpenApi3\\JsonSchema\\Model\\Schema') { - return method_exists($schema, 'getNullable') && !($schema->getNullable() ?? false); + return method_exists($schema, 'getNullable') && $schema->getNullable() === true; } return false;