Skip to content

Commit

Permalink
fix: Create deep copy before checking each sub schema in anyOf (#792)
Browse files Browse the repository at this point in the history
Fixes #711
  • Loading branch information
DannyvdSluijs authored Feb 26, 2025
1 parent 7eea9e4 commit 4406698
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add required permissions for welcome action ([#789](https://github.com/jsonrainbow/json-schema/pull/789))
- Upgrade php cs fixer to latest ([#783](https://github.com/jsonrainbow/json-schema/pull/783))
- Create deep copy before checking each sub schema in oneOf ([#791](https://github.com/jsonrainbow/json-schema/pull/791))
- Create deep copy before checking each sub schema in anyOf ([#792](https://github.com/jsonrainbow/json-schema/pull/792))

### Changed
- Used PHPStan's int-mask-of<T> type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))
Expand Down
8 changes: 6 additions & 2 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,15 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
if (isset($schema->anyOf)) {
$isValid = false;
$startErrors = $this->getErrors();
$coerceOrDefaults = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES | self::CHECK_MODE_APPLY_DEFAULTS);

foreach ($schema->anyOf as $anyOf) {
$initErrors = $this->getErrors();
try {
$this->checkUndefined($value, $anyOf, $path, $i);
if ($isValid = (count($this->getErrors()) == count($initErrors))) {
$anyOfValue = $coerceOrDefaults ? DeepCopy::copyOf($value) : $value;
$this->checkUndefined($anyOfValue, $anyOf, $path, $i);
if ($isValid = (count($this->getErrors()) === count($initErrors))) {
$value = $anyOfValue;
break;
}
} catch (ValidationException $e) {
Expand Down
44 changes: 44 additions & 0 deletions tests/Constraints/UndefinedConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,50 @@ public function getValidTests(): array
JSON
,
'checkMode' => Constraint::CHECK_MODE_COERCE_TYPES
],
'anyOf with apply defaults should not affect value passed to each sub schema (#711)' => [
'input' => <<<JSON
{
"b": 2
}
JSON
,
'schema' => <<<JSON
{
"anyOf": [
{
"required": [ "a" ],
"properties": {
"a": {
"type": "integer"
},
"aDefault": {
"type": "integer",
"default": 1
}
},
"type": "object",
"additionalProperties": false
},
{
"required": [ "b" ],
"properties": {
"b": {
"type": "integer"
},
"bDefault": {
"type": "integer",
"default": 2
}
},
"type": "object",
"additionalProperties": false
}
]
}
JSON
,
'checkMode' => Constraint::CHECK_MODE_APPLY_DEFAULTS
]
];
}
Expand Down

0 comments on commit 4406698

Please sign in to comment.