Skip to content

Commit 4406698

Browse files
fix: Create deep copy before checking each sub schema in anyOf (#792)
Fixes #711
1 parent 7eea9e4 commit 4406698

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Add required permissions for welcome action ([#789](https://github.com/jsonrainbow/json-schema/pull/789))
1414
- Upgrade php cs fixer to latest ([#783](https://github.com/jsonrainbow/json-schema/pull/783))
1515
- Create deep copy before checking each sub schema in oneOf ([#791](https://github.com/jsonrainbow/json-schema/pull/791))
16+
- Create deep copy before checking each sub schema in anyOf ([#792](https://github.com/jsonrainbow/json-schema/pull/792))
1617

1718
### Changed
1819
- Used PHPStan's int-mask-of<T> type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,15 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
333333
if (isset($schema->anyOf)) {
334334
$isValid = false;
335335
$startErrors = $this->getErrors();
336+
$coerceOrDefaults = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES | self::CHECK_MODE_APPLY_DEFAULTS);
337+
336338
foreach ($schema->anyOf as $anyOf) {
337339
$initErrors = $this->getErrors();
338340
try {
339-
$this->checkUndefined($value, $anyOf, $path, $i);
340-
if ($isValid = (count($this->getErrors()) == count($initErrors))) {
341+
$anyOfValue = $coerceOrDefaults ? DeepCopy::copyOf($value) : $value;
342+
$this->checkUndefined($anyOfValue, $anyOf, $path, $i);
343+
if ($isValid = (count($this->getErrors()) === count($initErrors))) {
344+
$value = $anyOfValue;
341345
break;
342346
}
343347
} catch (ValidationException $e) {

tests/Constraints/UndefinedConstraintTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,50 @@ public function getValidTests(): array
6969
JSON
7070
,
7171
'checkMode' => Constraint::CHECK_MODE_COERCE_TYPES
72+
],
73+
'anyOf with apply defaults should not affect value passed to each sub schema (#711)' => [
74+
'input' => <<<JSON
75+
{
76+
"b": 2
77+
}
78+
JSON
79+
,
80+
'schema' => <<<JSON
81+
{
82+
"anyOf": [
83+
{
84+
"required": [ "a" ],
85+
"properties": {
86+
"a": {
87+
"type": "integer"
88+
},
89+
"aDefault": {
90+
"type": "integer",
91+
"default": 1
92+
}
93+
},
94+
"type": "object",
95+
"additionalProperties": false
96+
},
97+
{
98+
"required": [ "b" ],
99+
"properties": {
100+
"b": {
101+
"type": "integer"
102+
},
103+
"bDefault": {
104+
"type": "integer",
105+
"default": 2
106+
}
107+
},
108+
"type": "object",
109+
"additionalProperties": false
110+
}
111+
]
112+
}
113+
JSON
114+
,
115+
'checkMode' => Constraint::CHECK_MODE_APPLY_DEFAULTS
72116
]
73117
];
74118
}

0 commit comments

Comments
 (0)