Skip to content

[BUGFIX] Apply defaults in $ref'ed property / item definitions #494

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

Merged
merged 3 commits into from
Feb 13, 2018
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
2 changes: 2 additions & 0 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ protected function applyDefaultValues(&$value, $schema, $path)
if (isset($schema->properties) && LooseTypeCheck::isObject($value)) {
// $value is an object or assoc array, and properties are defined - treat as an object
foreach ($schema->properties as $currentProperty => $propertyDefinition) {
$propertyDefinition = $this->factory->getSchemaStorage()->resolveRefSchema($propertyDefinition);
if (
!LooseTypeCheck::propertyExists($value, $currentProperty)
&& property_exists($propertyDefinition, 'default')
Expand All @@ -264,6 +265,7 @@ protected function applyDefaultValues(&$value, $schema, $path)
}
// $value is an array, and items are defined - treat as plain array
foreach ($items as $currentItem => $itemDefinition) {
$itemDefinition = $this->factory->getSchemaStorage()->resolveRefSchema($itemDefinition);
if (
!array_key_exists($currentItem, $value)
&& property_exists($itemDefinition, 'default')
Expand Down
4 changes: 2 additions & 2 deletions tests/Constraints/DefaultPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ public function getValidTests()
),
array(// #15 infinite recursion via $ref (object)
'{}',
'{"properties":{"propertyOne": {"$ref": "#","default": {}}}}',
'{"properties":{"propertyOne": {"$ref": "#","default": "valueOne"}}, "default": {}}',
'{"propertyOne":{}}'
),
array(// #16 infinite recursion via $ref (array)
'[]',
'{"items":[{"$ref":"#","default":[]}]}',
'{"items":[{"$ref":"#","default":"valueOne"}], "default": []}',
'[[]]'
),
array(// #17 default top value does not overwrite defined null
Expand Down