From 0158fec1f67bd21dcf9f539881cd2d8d14044590 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 3 Dec 2019 13:12:18 +0100 Subject: [PATCH] only allow $ref in array, a map may have a $ref property --- src/SpecBaseObject.php | 41 +++++++++++++++++++------------------- tests/spec/OpenApiTest.php | 5 ++++- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index aa231af..ecc1c87 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -73,31 +73,30 @@ public function __construct(array $data) $this->_errors[] = "property '$property' must be array, but " . gettype($data[$property]) . " given."; continue; } - if (isset($data[$property]['$ref'])) { - $this->_properties[$property] = new Reference($data[$property], null); - unset($data[$property]); - continue; - } switch (\count($type)) { case 1: - // array - $this->_properties[$property] = []; - foreach ($data[$property] as $item) { - if ($type[0] === Type::STRING) { - if (!is_string($item)) { - $this->_errors[] = "property '$property' must be array of strings, but array has " . gettype($item) . " element."; - } - $this->_properties[$property][] = $item; - } elseif (Type::isScalar($type[0])) { - $this->_properties[$property][] = $item; - } elseif ($type[0] === Type::ANY) { - if (is_array($item) && isset($item['$ref'])) { - $this->_properties[$property][] = new Reference($item, null); - } else { + if (isset($data[$property]['$ref'])) { + $this->_properties[$property] = new Reference($data[$property], null); + } else { + // array + $this->_properties[$property] = []; + foreach ($data[$property] as $item) { + if ($type[0] === Type::STRING) { + if (!is_string($item)) { + $this->_errors[] = "property '$property' must be array of strings, but array has " . gettype($item) . " element."; + } + $this->_properties[$property][] = $item; + } elseif (Type::isScalar($type[0])) { $this->_properties[$property][] = $item; + } elseif ($type[0] === Type::ANY) { + if (is_array($item) && isset($item['$ref'])) { + $this->_properties[$property][] = new Reference($item, null); + } else { + $this->_properties[$property][] = $item; + } + } else { + $this->_properties[$property][] = $this->instantiate($type[0], $item); } - } else { - $this->_properties[$property][] = $this->instantiate($type[0], $item); } } break; diff --git a/tests/spec/OpenApiTest.php b/tests/spec/OpenApiTest.php index 17cd8ed..80951cb 100644 --- a/tests/spec/OpenApiTest.php +++ b/tests/spec/OpenApiTest.php @@ -171,7 +171,10 @@ public function specProvider() $nexmoExamples ); foreach($all as $path) { - yield [substr($path, strlen(__DIR__ . '/../../vendor/')), basename($path)]; + yield [ + substr($path, strlen(__DIR__ . '/../../vendor/')), + basename(dirname($path, 2)) . DIRECTORY_SEPARATOR . basename(dirname($path, 1)) . DIRECTORY_SEPARATOR . basename($path) + ]; } }