Skip to content

Commit 6a13dfc

Browse files
committed
fix: create deep copy before checking each sub schema in oneOf when only check_mode_apply_defaults is set
#510
1 parent 649793d commit 6a13dfc

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,13 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
359359
$allErrors = [];
360360
$matchedSchemas = [];
361361
$startErrors = $this->getErrors();
362-
$coerce = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES);
362+
$coerceOrDefaults = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES | self::CHECK_MODE_APPLY_DEFAULTS);
363363

364364
foreach ($schema->oneOf as $oneOf) {
365365
try {
366366
$this->errors = [];
367367

368-
$oneOfValue = $coerce ? DeepCopy::copyOf($value) : $value;
368+
$oneOfValue = $coerceOrDefaults ? DeepCopy::copyOf($value) : $value;
369369
$this->checkUndefined($oneOfValue, $oneOf, $path, $i);
370370
if (count($this->getErrors()) === 0) {
371371
$matchedSchemas[] = ['schema' => $oneOf, 'value' => $oneOfValue];

tests/Constraints/UndefinedConstraintTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,56 @@ public function getValidTests(): array
7070
,
7171
'checkMode' => Constraint::CHECK_MODE_COERCE_TYPES
7272
],
73+
'oneOf with apply defaults should not affect value passed to each sub schema (#510)' => [
74+
'input' => <<<JSON
75+
{"foo": {"name": "bar"}}
76+
JSON
77+
,
78+
'schema' => <<<JSON
79+
{
80+
"oneOf": [
81+
{
82+
"type": "object",
83+
"properties": {
84+
"foo": {
85+
"type": "object",
86+
"properties": {
87+
"name": {"enum":["baz"],"default":"baz"},
88+
"meta": {"enum":["baz"],"default":"baz"}
89+
}
90+
}
91+
}
92+
},
93+
{
94+
"type": "object",
95+
"properties": {
96+
"foo": {
97+
"type": "object",
98+
"properties": {
99+
"name": {"enum":["bar"],"default":"bar"},
100+
"meta": {"enum":["bar"],"default":"bar"}
101+
}
102+
}
103+
}
104+
},
105+
{
106+
"type": "object",
107+
"properties": {
108+
"foo": {
109+
"type": "object",
110+
"properties": {
111+
"name": {"enum":["zip"],"default":"zip"},
112+
"meta": {"enum":["zip"],"default":"zip"}
113+
}
114+
}
115+
}
116+
}
117+
]
118+
}
119+
JSON
120+
,
121+
'checkMode' => Constraint::CHECK_MODE_APPLY_DEFAULTS
122+
],
73123
'anyOf with apply defaults should not affect value passed to each sub schema (#711)' => [
74124
'input' => <<<JSON
75125
{

0 commit comments

Comments
 (0)