Skip to content

Fix OpenAPI v3.0 Schema Violation: Array value found, but an object is required #125

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 12 additions & 7 deletions src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
22 changes: 22 additions & 0 deletions tests/spec/data/empty-maps.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
}
}
}
}
}