Skip to content

Commit 331fd7d

Browse files
thejamescollinscebe
authored andcommitted
During serialization convert empty Map fields to empty Objects
Otherwise these fail validation against the OpenAPI v3.0 schema.
1 parent 874984d commit 331fd7d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/SpecBaseObject.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,19 @@ public function getSerializableData()
191191
$data[$k] = $v->getSerializableData();
192192
} elseif (is_array($v)) {
193193
$toObject = false;
194-
$j = 0;
195-
foreach ($v as $i => $d) {
196-
if ($j++ !== $i) {
197-
$toObject = true;
198-
}
199-
if ($d instanceof SpecObjectInterface) {
200-
$data[$k][$i] = $d->getSerializableData();
194+
if (!empty($v)) {
195+
$j = 0;
196+
foreach ($v as $i => $d) {
197+
if ($j++ !== $i) {
198+
$toObject = true;
199+
}
200+
if ($d instanceof SpecObjectInterface) {
201+
$data[$k][$i] = $d->getSerializableData();
202+
}
201203
}
204+
} elseif (isset($this->attributes()[$k]) && is_array($this->attributes()[$k]) && 2 === count($this->attributes()[$k])) {
205+
// An empty Map, which is an empty array in PHP but needs to be an empty object in output.
206+
$toObject = true;
202207
}
203208
if ($toObject) {
204209
$data[$k] = (object) $data[$k];

0 commit comments

Comments
 (0)