diff --git a/src/Phpro/SoapClient/CodeGenerator/Assembler/PropertyAssembler.php b/src/Phpro/SoapClient/CodeGenerator/Assembler/PropertyAssembler.php index f488f6fc..6ebe0538 100644 --- a/src/Phpro/SoapClient/CodeGenerator/Assembler/PropertyAssembler.php +++ b/src/Phpro/SoapClient/CodeGenerator/Assembler/PropertyAssembler.php @@ -9,6 +9,7 @@ use Phpro\SoapClient\Exception\AssemblerException; use Laminas\Code\Generator\PropertyGenerator; use Soap\Engine\Metadata\Model\TypeMeta; +use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType; /** * Class PropertyAssembler @@ -57,7 +58,8 @@ public function assemble(ContextInterface $context) $propertyGenerator = PropertyGenerator::fromArray([ 'name' => $property->getName(), 'visibility' => $this->options->visibility(), - 'omitdefaultvalue' => !$this->options->useOptionalValue(), + 'omitdefaultvalue' => !$this->options->useOptionalValue() + && !(new IsConsideredNullableType())($property->getMeta()), ]); if ($this->options->useDocBlocks()) { diff --git a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/PropertyAssemblerTest.php b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/PropertyAssemblerTest.php index e3d92474..57def086 100644 --- a/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/PropertyAssemblerTest.php +++ b/test/PhproTest/SoapClient/Unit/CodeGenerator/Assembler/PropertyAssemblerTest.php @@ -265,6 +265,35 @@ class MyType $this->assertEquals($expected, $code); } + /** + * @test + */ + function it_assembles_property_with_null() + { + $assembler = new PropertyAssembler( + PropertyAssemblerOptions::create() + ); + $context = $this->createContextWithNullableType(); + $assembler->assemble($context); + $code = $context->getClass()->generate(); + $expected = <<assertEquals($expected, $code); + } + /** * @return PropertyContext @@ -295,4 +324,19 @@ private function createContextWithLongType() ], new TypeMeta()); return new PropertyContext($class, $type, $property); } + + /** + * @return PropertyContext + */ + private function createContextWithNullableType() + { + $class = new ClassGenerator('MyType', 'MyNamespace'); + $type = new Type('MyNamespace', 'MyType', [ + $property = Property::fromMetaData('ns1', new MetaProperty('prop1', XsdType::guess('string')->withMeta( + static fn (TypeMeta $meta): TypeMeta => $meta->withDocs('Type specific docs')->withIsNullable(true) + ))), + ], new TypeMeta()); + + return new PropertyContext($class, $type, $property); + } }