From a94f60c332e0fa395f5682112d730ef89afee04d Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Wed, 26 Feb 2025 19:41:16 +0100 Subject: [PATCH] fix: create deep copy before checking each sub schema in oneOf when only check_mode_apply_defaults is set (#795) Fixes https://github.com/jsonrainbow/json-schema/issues/510 --- CHANGELOG.md | 1 + .../Constraints/UndefinedConstraint.php | 4 +- tests/Constraints/UndefinedConstraintTest.php | 50 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40c06e5..53a92fc 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)) diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 2c03af9..b9e0012 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -359,13 +359,13 @@ class UndefinedConstraint extends Constraint $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 b038eb9..7e66a60 100644 --- a/tests/Constraints/UndefinedConstraintTest.php +++ b/tests/Constraints/UndefinedConstraintTest.php @@ -70,6 +70,56 @@ JSON , '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' => <<