diff --git a/Makefile b/Makefile index 627df52f..9467e0ab 100644 --- a/Makefile +++ b/Makefile @@ -24,11 +24,13 @@ test: php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --verbose --colors=always $(TESTCASE) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml + php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/empty-maps.json lint: php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/reference/playlist.json php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml + php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/empty-maps.json node_modules/.bin/speccy lint tests/spec/data/reference/playlist.json node_modules/.bin/speccy lint tests/spec/data/recursion.json diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index 29f70b42..6dbd9488 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -191,14 +191,19 @@ public function getSerializableData() $data[$k] = $v->getSerializableData(); } elseif (is_array($v)) { $toObject = false; - $j = 0; - foreach ($v as $i => $d) { - if ($j++ !== $i) { - $toObject = true; - } - if ($d instanceof SpecObjectInterface) { - $data[$k][$i] = $d->getSerializableData(); + if (!empty($v)) { + $j = 0; + foreach ($v as $i => $d) { + if ($j++ !== $i) { + $toObject = true; + } + if ($d instanceof SpecObjectInterface) { + $data[$k][$i] = $d->getSerializableData(); + } } + } elseif (isset($this->attributes()[$k]) && is_array($this->attributes()[$k]) && 2 === count($this->attributes()[$k])) { + // An empty Map, which is an empty array in PHP but needs to be an empty object in output. + $toObject = true; } if ($toObject) { $data[$k] = (object) $data[$k]; diff --git a/tests/spec/data/empty-maps.json b/tests/spec/data/empty-maps.json new file mode 100644 index 00000000..51b1eee7 --- /dev/null +++ b/tests/spec/data/empty-maps.json @@ -0,0 +1,22 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "test", + "version": "1.0" + }, + "paths": { + "/products": { + "description": "default", + "get": { + "responses": { + "200": { + "description": "Products", + "headers": {}, + "content": {}, + "links": {} + } + } + } + } + } +} \ No newline at end of file