Skip to content

Commit e0baac8

Browse files
committed
Also fix infinite recursion via $ref for array defaults
1 parent 37aa991 commit e0baac8

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
122122

123123
// Apply default values from schema
124124
if (!$path->fromDefault()) {
125-
$this->applyDefaultValues($value, $schema);
125+
$this->applyDefaultValues($value, $schema, $path);
126126
}
127127

128128
// Verify required values
@@ -189,10 +189,11 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
189189
/**
190190
* Apply default values
191191
*
192-
* @param mixed $value
193-
* @param mixed $schema
192+
* @param mixed $value
193+
* @param mixed $schema
194+
* @param JsonPointer $path
194195
*/
195-
protected function applyDefaultValues(&$value, $schema)
196+
protected function applyDefaultValues(&$value, $schema, $path)
196197
{
197198
// only apply defaults if feature is enabled
198199
if (!$this->factory->getConfig(self::CHECK_MODE_APPLY_DEFAULTS)) {
@@ -251,10 +252,12 @@ protected function applyDefaultValues(&$value, $schema)
251252
$value[$currentItem] = $itemDefinition->default;
252253
}
253254
}
255+
$path->setFromDefault();
254256
}
255257
} elseif (($value instanceof self || $value === null) && isset($schema->default) && $shouldApply($schema)) {
256258
// $value is a leaf, not a container - apply the default directly
257259
$value = is_object($schema->default) ? clone $schema->default : $schema->default;
260+
$path->setFromDefault();
258261
}
259262
}
260263

tests/Constraints/DefaultPropertiesTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,17 @@ public function getValidTests()
112112
'{"propertyTwo":"valueTwo"}',
113113
Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS
114114
),
115-
array(// #16 infinite recursion via $ref
115+
array(// #16 infinite recursion via $ref (object)
116116
'{}',
117117
'{"properties":{"propertyOne": {"$ref": "#","default": {}}}}',
118118
'{"propertyOne":{}}'
119119
),
120-
array(// #17 default value for null
120+
array(// #17 infinite recursion via $ref (array)
121+
'[]',
122+
'{"items":[{"$ref":"#","default":[]}]}',
123+
'[[]]'
124+
),
125+
array(// #18 default value for null
121126
'null',
122127
'{"default":"valueOne"}',
123128
'"valueOne"'

0 commit comments

Comments
 (0)