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

fix bug when applying defaults #405

Merged
merged 1 commit into from
May 5, 2017
Merged
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
8 changes: 7 additions & 1 deletion src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,14 @@ protected function applyDefaultValues(&$value, $schema, $path)
}
}
} elseif (isset($schema->items) && LooseTypeCheck::isArray($value)) {
$items = array();
if (LooseTypeCheck::isArray($schema->items)) {
$items = $schema->items;
} elseif (isset($schema->minItems) && count($value) < $schema->minItems) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to check and handle the state of $schema->exclusiveMinimum.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I has no idea this existed, I'll add a test for exclusiveMinimum

$items = array_fill(count($value), $schema->minItems - count($value), $schema->items);
}
// $value is an array, and items are defined - treat as plain array
foreach ($schema->items as $currentItem => $itemDefinition) {
foreach ($items as $currentItem => $itemDefinition) {
if (
!array_key_exists($currentItem, $value)
&& property_exists($itemDefinition, 'default')
Expand Down
15 changes: 15 additions & 0 deletions tests/Constraints/DefaultPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ public function getValidTests()
'{"items":[{"default":null}]}',
'[null]'
),
array(// #21 items might be a schema (instead of an array of schema)
'[{}]',
'{"items":{"properties":{"propertyOne":{"default":"valueOne"}}}}',
'[{"propertyOne":"valueOne"}]'
),
array(// #22 if items is not an array, it does not create a new item
'[]',
'{"items":{"properties":{"propertyOne":{"default":"valueOne"}}}}',
'[]'
),
array(// #23 if items is a schema with a default value and minItems is present, fill the array
'["a"]',
'{"items":{"default":"b"}, "minItems": 3}',
'["a","b","b"]'
),
);
}

Expand Down