From a13a11115b40d335e335b938a9c150c736165611 Mon Sep 17 00:00:00 2001 From: James Collins Date: Fri, 8 Oct 2021 15:00:29 +0800 Subject: [PATCH 1/2] Add test to demonstrate OpenAPI v3.0 schema validation fails with empty Path properties --- Makefile | 2 ++ tests/spec/data/empty-maps.json | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/spec/data/empty-maps.json 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/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 From 4cde8a128c8577feedbc401fd820fca003305aeb Mon Sep 17 00:00:00 2001 From: James Collins Date: Fri, 8 Oct 2021 15:13:53 +0800 Subject: [PATCH 2/2] During serialization convert empty Map fields to empty Objects Otherwise these fail validation against the OpenAPI v3.0 schema. --- src/SpecBaseObject.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) 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];