Closed lstrojny closed 6 months ago
This should do the trick:
$propertyGenerator = PropertyGenerator::fromArray([
'name' => $property->getName(),
'visibility' => $this->options->visibility(),
- 'omitdefaultvalue' => !$this->options->useOptionalValue(),
+ 'omitdefaultvalue' => !$this->options->useOptionalValue() && !(new IsConsideredNullableType())($property->getMeta()),
]);
Thanks for reporting @lstrojny,
Could you give me an example in which scenario you get a 'uninitialized property' error? Is it in the Request or Response side or maybe in both?
I'm not opposed to having that fix, but I would love to understand where your error comes from exactly first.
It’s for a response with an element like this:
<xs:element name="SomeProp" type="tns:SomeType" minOccurs="0"/>
When I get 0 occurances of SomeProp, the generated PHP code will bail with "Typed property …::$SomeProp must not be accessed before initialization"
That could make sense indeed.
Care to provide a PR for this?
I assume the fix is rather omit = !(useOptionalValue || nullable)
making it null if the property is null or if the configuration tells to add optional values.
@veewee done
Bug Report
Summary
PropertyAssembler should assign null as a default value for nullable properties.
Current behavior
PropertyAssembler can be configured to assume that all properties are nullable but it will not correctly check for nullability and only then assign null as a default.
How to reproduce
Use
PropertyAssembler
as is with a nullable property and the generated code will beprivate ?Type $prop;
.Expected behavior
Use
PropertyAssembler
as is with a nullable property and the generated code will beprivate ?Type $prop = null;
to prevent "uninitialized property" errors.