diff --git a/src/PropertyProcessor/Property/AbstractPropertyProcessor.php b/src/PropertyProcessor/Property/AbstractPropertyProcessor.php index 03f4b4ec..e041c810 100644 --- a/src/PropertyProcessor/Property/AbstractPropertyProcessor.php +++ b/src/PropertyProcessor/Property/AbstractPropertyProcessor.php @@ -117,7 +117,7 @@ static function ($value): string { $property->addTypeHintDecorator(new TypeHintDecorator($typesOfEnum)); } - if ($this->isImplicitNullAllowed($property) && !in_array(null, $allowedValues)) { + if ($this->isImplicitNullAllowed($property) && !in_array(null, $allowedValues, true)) { $allowedValues[] = null; } diff --git a/tests/Objects/EnumPropertyTest.php b/tests/Objects/EnumPropertyTest.php index c337e55b..e1a96a91 100644 --- a/tests/Objects/EnumPropertyTest.php +++ b/tests/Objects/EnumPropertyTest.php @@ -223,10 +223,29 @@ public function validEnumEntriesUntypedEnumDataProvider(): array return [ "string 'red'" => ['red'], 'null' => [null], + 'int 0' => [0], 'int 10' => [10], ]; } + /** + * @throws FileSystemException + * @throws RenderException + * @throws SchemaException + */ + public function testSuccessCreateObjectWithOptionalFieldsContainingZero(): void + { + $className = $this->generateClassFromFile('TypedEnumPropertyWithZeroValue.json', null, false, true); + $object = new $className(['property' => 10]); + + $this->assertSame(10, $object->getProperty()); + $this->assertSame(null, $object->getPropertyWithZero()); + + $returnType = $this->getReturnType($object, 'getPropertyWithZero'); + $this->assertSame('int', $returnType->getName()); + $this->assertTrue($returnType->allowsNull()); + } + /** * @throws FileSystemException * @throws RenderException diff --git a/tests/Schema/EnumPropertyTest/TypedEnumPropertyWithZeroValue.json b/tests/Schema/EnumPropertyTest/TypedEnumPropertyWithZeroValue.json new file mode 100644 index 00000000..f053638f --- /dev/null +++ b/tests/Schema/EnumPropertyTest/TypedEnumPropertyWithZeroValue.json @@ -0,0 +1,13 @@ +{ + "type": "object", + "properties": { + "property": { + "type": "integer", + "enum": [10, 20] + }, + "propertyWithZero": { + "type": "integer", + "enum": [0, 1] + } + } +} \ No newline at end of file diff --git a/tests/Schema/EnumPropertyTest/UntypedEnumProperty.json b/tests/Schema/EnumPropertyTest/UntypedEnumProperty.json index 94cb54d8..0bab557c 100644 --- a/tests/Schema/EnumPropertyTest/UntypedEnumProperty.json +++ b/tests/Schema/EnumPropertyTest/UntypedEnumProperty.json @@ -2,7 +2,7 @@ "type": "object", "properties": { "property": { - "enum": ["red", 10] + "enum": ["red", 0, 10] } } } \ No newline at end of file