Skip to content

Commit 1bb1374

Browse files
Added strict validation
Added strict validation because null can implicitly convert to zero, and that didn't work for enums containing zero.
1 parent 61dbcde commit 1bb1374

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/PropertyProcessor/Property/AbstractPropertyProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static function ($value): string {
117117
$property->addTypeHintDecorator(new TypeHintDecorator($typesOfEnum));
118118
}
119119

120-
if ($this->isImplicitNullAllowed($property) && !in_array(null, $allowedValues)) {
120+
if ($this->isImplicitNullAllowed($property) && !in_array(null, $allowedValues, true)) {
121121
$allowedValues[] = null;
122122
}
123123

tests/Objects/EnumPropertyTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,29 @@ public function validEnumEntriesUntypedEnumDataProvider(): array
223223
return [
224224
"string 'red'" => ['red'],
225225
'null' => [null],
226+
'int 0' => [0],
226227
'int 10' => [10],
227228
];
228229
}
229230

231+
/**
232+
* @throws FileSystemException
233+
* @throws RenderException
234+
* @throws SchemaException
235+
*/
236+
public function testSuccessCreateObjectWithOptionalFieldsContainingZero(): void
237+
{
238+
$className = $this->generateClassFromFile('TypedEnumPropertyWithZeroValue.json', null, false, true);
239+
$object = new $className(['property' => 10]);
240+
241+
$this->assertSame(10, $object->getProperty());
242+
$this->assertSame(null, $object->getPropertyWithZero());
243+
244+
$returnType = $this->getReturnType($object, 'getPropertyWithZero');
245+
$this->assertSame('int', $returnType->getName());
246+
$this->assertTrue($returnType->allowsNull());
247+
}
248+
230249
/**
231250
* @throws FileSystemException
232251
* @throws RenderException
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"property": {
5+
"type": "integer",
6+
"enum": [10, 20]
7+
},
8+
"propertyWithZero": {
9+
"type": "integer",
10+
"enum": [0, 1]
11+
}
12+
}
13+
}

tests/Schema/EnumPropertyTest/UntypedEnumProperty.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "object",
33
"properties": {
44
"property": {
5-
"enum": ["red", 10]
5+
"enum": ["red", 0, 10]
66
}
77
}
88
}

0 commit comments

Comments
 (0)