Skip to content

Commit 1bd42dd

Browse files
committed
add comments, optimize tests for #125
1 parent 331fd7d commit 1bd42dd

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ install: composer.lock yarn.lock
4141
$(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi
4242
$(DOCKER_NODE) yarn install
4343

44-
test:
44+
test: unit test-recursion.json test-recursion2.yaml test-empty-maps.json
45+
46+
unit:
4547
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --verbose --colors=always $(TESTCASE)
46-
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
47-
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
48-
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/empty-maps.json
48+
49+
# test specific JSON files in tests/spec/data/
50+
# e.g. test-recursion will run validation on tests/spec/data/recursion.json
51+
test-%: tests/spec/data/%
52+
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate $<
4953

5054
lint: install
5155
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/reference/playlist.json

src/SpecBaseObject.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ public function getSerializableData()
190190
if ($v instanceof SpecObjectInterface) {
191191
$data[$k] = $v->getSerializableData();
192192
} elseif (is_array($v)) {
193+
// test if php arrays should be represented as object in YAML/JSON
193194
$toObject = false;
194195
if (!empty($v)) {
196+
// case 1: non-empty array should be an object if it does not contain
197+
// consecutive numeric keys
195198
$j = 0;
196199
foreach ($v as $i => $d) {
197200
if ($j++ !== $i) {
@@ -202,7 +205,7 @@ public function getSerializableData()
202205
}
203206
}
204207
} elseif (isset($this->attributes()[$k]) && is_array($this->attributes()[$k]) && 2 === count($this->attributes()[$k])) {
205-
// An empty Map, which is an empty array in PHP but needs to be an empty object in output.
208+
// case 2: Attribute type is an object (specified in attributes() by an array which specifies two items (key and value type)
206209
$toObject = true;
207210
}
208211
if ($toObject) {

0 commit comments

Comments
 (0)