Skip to content

Commit c1818da

Browse files
committed
Also fix infinite recursion via $ref for array defaults
1 parent 5a331b7 commit c1818da

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
@@ -121,7 +121,7 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
121121

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

127127
// Verify required values
@@ -187,10 +187,11 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
187187
/**
188188
* Apply default values
189189
*
190-
* @param mixed $value
191-
* @param mixed $schema
190+
* @param mixed $value
191+
* @param mixed $schema
192+
* @param JsonPointer $path
192193
*/
193-
protected function applyDefaultValues(&$value, $schema)
194+
protected function applyDefaultValues(&$value, $schema, $path)
194195
{
195196
// only apply defaults if feature is enabled
196197
if (!$this->factory->getConfig(self::CHECK_MODE_APPLY_DEFAULTS)) {
@@ -249,10 +250,12 @@ protected function applyDefaultValues(&$value, $schema)
249250
$value[$currentItem] = $itemDefinition->default;
250251
}
251252
}
253+
$path->setFromDefault();
252254
}
253255
} elseif (($value instanceof self || $value === null) && isset($schema->default) && $shouldApply($schema)) {
254256
// $value is a leaf, not a container - apply the default directly
255257
$value = is_object($schema->default) ? clone $schema->default : $schema->default;
258+
$path->setFromDefault();
256259
}
257260
}
258261

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)