Skip to content

Commit

Permalink
Set nullable true when default value is null
Browse files Browse the repository at this point in the history
When reflection shows that the default parameter of a a property is
null, then the `nullable` parameter is set to `true` for that property.

Currently that needs to be set via the attribute but that is a bit
redundant as the information is already available from reflection

This change only does that for properties and for setter methods.

Right now getter methods that allow a null value to be returned are not
taken into account for setting the `nullable` property.
  • Loading branch information
heiglandreas committed Nov 11, 2024
1 parent c431718 commit 4357ef7
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/ModelDescriber/Annotations/ReflectionReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ public function updateProperty(
$methodDefault = $this->getDefaultFromMethodReflection($reflection);
if (Generator::UNDEFINED !== $methodDefault) {
$property->default = $methodDefault;
if (null === $methodDefault) {
$property->nullable = true;
}

return;
}
}

if ($reflection instanceof \ReflectionProperty) {
$methodDefault = $this->getDefaultFromPropertyReflection($reflection);
if (Generator::UNDEFINED !== $methodDefault) {
$property->default = $methodDefault;
$propertyDefault = $this->getDefaultFromPropertyReflection($reflection);
if (Generator::UNDEFINED !== $propertyDefault) {
$property->default = $propertyDefault;
if (null === $propertyDefault) {
$property->nullable = true;
}

return;
}
Expand All @@ -90,6 +96,9 @@ public function updateProperty(
}

$property->default = $parameter->getDefaultValue();
if (null === $property->default) {
$property->nullable = true;
}
}
}

Expand Down Expand Up @@ -117,10 +126,6 @@ private function getDefaultFromMethodReflection(\ReflectionMethod $reflection)
return Generator::UNDEFINED;
}

if (null === $param->getDefaultValue()) {
return Generator::UNDEFINED;
}

return $param->getDefaultValue();
}

Expand All @@ -136,10 +141,6 @@ public function getDefaultFromPropertyReflection(\ReflectionProperty $reflection

$defaultValue = $reflection->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null;

if (null === $defaultValue) {
return Generator::UNDEFINED;
}

return $defaultValue;
}
}

0 comments on commit 4357ef7

Please sign in to comment.