Skip to content

Commit

Permalink
Merge pull request #494 from erayd/apply-ref-default
Browse files Browse the repository at this point in the history
[BUGFIX] Apply defaults in $ref'ed property / item definitions
  • Loading branch information
bighappyface committed Feb 13, 2018
2 parents 7d7f43a + 8a5ac14 commit dd084db
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
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
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

0 comments on commit dd084db

Please sign in to comment.