Skip to content

Commit

Permalink
Added strict validation
Browse files Browse the repository at this point in the history
Added strict validation because null can implicitly convert to zero, and that didn't work for enums containing zero.
  • Loading branch information
kolodkinvalentin authored Sep 6, 2024
1 parent 61dbcde commit 1bb1374
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Objects/EnumPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/Schema/EnumPropertyTest/TypedEnumPropertyWithZeroValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "object",
"properties": {
"property": {
"type": "integer",
"enum": [10, 20]
},
"propertyWithZero": {
"type": "integer",
"enum": [0, 1]
}
}
}
2 changes: 1 addition & 1 deletion tests/Schema/EnumPropertyTest/UntypedEnumProperty.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "object",
"properties": {
"property": {
"enum": ["red", 10]
"enum": ["red", 0, 10]
}
}
}

0 comments on commit 1bb1374

Please sign in to comment.