Skip to content

Commit 45d2dfb

Browse files
committed
Improve nullable property handling in data export
1 parent e612435 commit 45d2dfb

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/Structure/ClassStructureTrait.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,23 @@ public function jsonSerialize()
9999
{
100100
$result = new \stdClass();
101101
$schema = static::schema();
102-
$classname = $schema->getObjectItemClass();
103-
$classReference = (class_exists($classname)) ? new $classname() : new \stdClass();
104102
$properties = $schema->getProperties();
105103
if (null !== $properties) {
106104
foreach ($properties->getDataKeyMap() as $propertyName => $dataName) {
107105
$value = $this->$propertyName;
108-
$types = ($schema->getProperty($propertyName) instanceof Schema)
109-
? $schema->getProperty($propertyName)->type
110-
: null;
111-
if (
112-
(
113-
null !== $value
114-
||
115-
($value === null
116-
&& is_array($types)
117-
&& in_array('null', $types)
118-
)
119-
)
120-
|| array_key_exists($propertyName, $this->__arrayOfData)
121-
) {
106+
// Value is exported if exists.
107+
if (null !== $value || array_key_exists($propertyName, $this->__arrayOfData)) {
122108
$result->$dataName = $value;
109+
continue;
110+
}
111+
112+
// Non-existent value is only exported if belongs to nullable property (having 'null' in type array).
113+
$property = $schema->getProperty($propertyName);
114+
if ($property instanceof Schema) {
115+
$types = $property->type;
116+
if ($types === Schema::NULL || (is_array($types) && in_array(Schema::NULL, $types))) {
117+
$result->$dataName = $value;
118+
}
123119
}
124120
}
125121
}

0 commit comments

Comments
 (0)