Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[openapi v3.1] Preserve property item keys #179

Open
wants to merge 1 commit into
base: openapi-31
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/spec/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}