From 5c2c67351a90dc1cd8c6b8ea3a00c8554f9cfafb Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Wed, 26 Feb 2025 14:28:40 +0100 Subject: [PATCH 1/2] fix: Create deep copy before checking each sub schema in anyOf https://github.com/jsonrainbow/json-schema/issues/711 --- .../Constraints/UndefinedConstraint.php | 8 +++- tests/Constraints/UndefinedConstraintTest.php | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 9b6c68ad..2c03af94 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -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) { diff --git a/tests/Constraints/UndefinedConstraintTest.php b/tests/Constraints/UndefinedConstraintTest.php index a996be26..b038eb91 100644 --- a/tests/Constraints/UndefinedConstraintTest.php +++ b/tests/Constraints/UndefinedConstraintTest.php @@ -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' => << << Constraint::CHECK_MODE_APPLY_DEFAULTS ] ]; } From cc4449227b746f5d7ed2b3ce87521df3e1990b0a Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Wed, 26 Feb 2025 14:43:34 +0100 Subject: [PATCH 2/2] docs: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54c0f46c..2365ad9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))