diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index 4e57a5e..919da7e 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -86,22 +86,22 @@ public function __construct(array $data) } else { // array $this->_properties[$property] = []; - foreach ($data[$property] as $item) { + foreach ($data[$property] as $key => $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; + $this->_properties[$property][$key] = $item; } elseif (Type::isScalar($type[0])) { - $this->_properties[$property][] = $item; + $this->_properties[$property][$key] = $item; } elseif ($type[0] === Type::ANY) { if (is_array($item) && isset($item['$ref'])) { - $this->_properties[$property][] = new Reference($item, null); + $this->_properties[$property][$key] = new Reference($item, null); } else { - $this->_properties[$property][] = $item; + $this->_properties[$property][$key] = $item; } } else { - $this->_properties[$property][] = $this->instantiate($type[0], $item); + $this->_properties[$property][$key] = $this->instantiate($type[0], $item); } } } diff --git a/tests/spec/SchemaTest.php b/tests/spec/SchemaTest.php index 1600b3b..4c307b2 100644 --- a/tests/spec/SchemaTest.php +++ b/tests/spec/SchemaTest.php @@ -420,4 +420,31 @@ public function testPropertyNameRef() $this->assertEquals('string', $person->properties['name']->type); $this->assertEquals('string', $person->properties['$ref']->type); } + + public function testArrayKeyIsPerseveredInPropertiesThatAreArrays() + { + $json = <<<'JSON' +{ + "webhooks": { + "branch-protection-rule-created": { + "post": { + "description": "A branch protection rule was created.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + } + } + } + } + } +} +JSON; + $openApi = Reader::readFromJson($json); + self::assertArrayHasKey('branch-protection-rule-created', $openApi->webhooks); + } }