Skip to content

Commit 28169a3

Browse files
committed
Transform default values if no value is provided
1 parent b21ac59 commit 28169a3

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

src/Templates/Model.phptpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class {{ class }} implements \PHPModelGenerator\Interfaces\JSONModelInterface
151151
{
152152
{% if not generatorConfiguration.isImplicitNullAllowed() %}
153153
{% if not property.isRequired() %}
154-
if (!array_key_exists('{{ property.getName() }}', $modelData)) {
154+
if (!array_key_exists('{{ property.getName() }}', $modelData) && $this->{{ property.getAttribute() }} === null) {
155155
return;
156156
}
157157
{% endif %}

tests/Basic/FilterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,4 +758,21 @@ public function testEnumCheckWithTransformingFilterIsNotExecutedForTransformedVa
758758
$object->getFilteredProperty()->format(DATE_ATOM)
759759
);
760760
}
761+
762+
/**
763+
* @dataProvider implicitNullDataProvider
764+
*
765+
* @param bool $implicitNull
766+
*/
767+
public function testDefaultValuesAreTransformed(bool $implicitNull): void
768+
{
769+
$className = $this->generateClassFromFile('DefaultValueFilter.json', null, false, $implicitNull);
770+
$object = new $className();
771+
772+
$this->assertInstanceOf(DateTime::class, $object->getCreated());
773+
$this->assertSame(
774+
(new DateTime('2020-12-12'))->format(DATE_ATOM),
775+
$object->getCreated()->format(DATE_ATOM)
776+
);
777+
}
761778
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"created": {
5+
"default": "2020-12-12",
6+
"type": "string",
7+
"filter": "dateTime"
8+
}
9+
}
10+
}

tests/manual/schema/person.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"type": "object",
44
"properties": {
55
"name": {
6-
"type": ["null", "string"],
6+
"type": "string",
77
"description": "The name of the person",
8-
"example": "Lawrence",
9-
"filter": [
10-
"trim",
11-
"dateTime"
12-
]
8+
"example": "Lawrence"
9+
},
10+
"age": {
11+
"type": "integer",
12+
"description": "The age of the person",
13+
"example": 42
1314
}
1415
},
1516
"required": [

tests/manual/test.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
$generator = new ModelGenerator((new GeneratorConfiguration())
1010
->setNamespacePrefix('\\ManualSchema')
1111
->setImmutable(false)
12-
->setPrettyPrint(false)
13-
->setImplicitNull(false)
12+
->setPrettyPrint(true)
1413
);
1514

1615
$generator

0 commit comments

Comments
 (0)