Skip to content

Commit 0771bff

Browse files
author
Mathieu Rochette
committed
fix bug when applying defaults for array items when the schema is for
all items
1 parent 2553ebd commit 0771bff

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ protected function applyDefaultValues(&$value, $schema, $path)
252252
$this->appliedDefaults[] = $currentProperty;
253253
}
254254
}
255-
} elseif (isset($schema->items) && LooseTypeCheck::isArray($value)) {
255+
} elseif (
256+
isset($schema->items) &&
257+
LooseTypeCheck::isArray($schema->items) &&
258+
LooseTypeCheck::isArray($value)
259+
) {
256260
// $value is an array, and items are defined - treat as plain array
257261
foreach ($schema->items as $currentItem => $itemDefinition) {
258262
if (

tests/Constraints/DefaultPropertiesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ public function getValidTests()
149149
'{"items":[{"default":null}]}',
150150
'[null]'
151151
),
152+
array(// #21 items might be an array of schema (instead of an array of schema)
153+
'[{}]',
154+
'{"items":{"properties":{"propertyOne":{"default":"valueOne"}},"required":[]}}',
155+
'[{"propertyOne":"valueOne"}]'
156+
),
157+
array(// #22 if items is not an array, it does not create new item
158+
'[]',
159+
'{"items":{"properties":{"propertyOne":{"default":"valueOne"}},"required":[]}}',
160+
'[]'
161+
),
152162
);
153163
}
154164

0 commit comments

Comments
 (0)