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), 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;