From 6a13dfc2629ab409126b457aad63bc5c506dcd46 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Wed, 26 Feb 2025 16:54:06 +0100 Subject: [PATCH 1/2] fix: create deep copy before checking each sub schema in oneOf when only check_mode_apply_defaults is set https://github.com/jsonrainbow/json-schema/issues/510 --- .../Constraints/UndefinedConstraint.php | 4 +- tests/Constraints/UndefinedConstraintTest.php | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 2c03af94..b9e00121 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -359,13 +359,13 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i $allErrors = []; $matchedSchemas = []; $startErrors = $this->getErrors(); - $coerce = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES); + $coerceOrDefaults = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES | self::CHECK_MODE_APPLY_DEFAULTS); foreach ($schema->oneOf as $oneOf) { try { $this->errors = []; - $oneOfValue = $coerce ? DeepCopy::copyOf($value) : $value; + $oneOfValue = $coerceOrDefaults ? DeepCopy::copyOf($value) : $value; $this->checkUndefined($oneOfValue, $oneOf, $path, $i); if (count($this->getErrors()) === 0) { $matchedSchemas[] = ['schema' => $oneOf, 'value' => $oneOfValue]; diff --git a/tests/Constraints/UndefinedConstraintTest.php b/tests/Constraints/UndefinedConstraintTest.php index b038eb91..7e66a60b 100644 --- a/tests/Constraints/UndefinedConstraintTest.php +++ b/tests/Constraints/UndefinedConstraintTest.php @@ -70,6 +70,56 @@ public function getValidTests(): array , 'checkMode' => Constraint::CHECK_MODE_COERCE_TYPES ], + 'oneOf with apply defaults should not affect value passed to each sub schema (#510)' => [ + 'input' => << << Constraint::CHECK_MODE_APPLY_DEFAULTS + ], 'anyOf with apply defaults should not affect value passed to each sub schema (#711)' => [ 'input' => << Date: Wed, 26 Feb 2025 16:55:35 +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 40c06e51..53a92fc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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)) - Correctly set the schema ID when passing it as assoc array ([#794](https://github.com/jsonrainbow/json-schema/pull/794)) +- Create deep copy before checking each sub schema in oneOf when only check_mode_apply_defaults is set ([#795](https://github.com/jsonrainbow/json-schema/pull/795)) ### Changed - Used PHPStan's int-mask-of type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))