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
This commit is contained in:
Danny van der Sluijs 2025-02-26 19:41:16 +01:00 committed by GitHub
parent 649793d2b9
commit a94f60c332
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 2 deletions

View File

@ -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<T> type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))

View File

@ -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];

View File

@ -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' => <<<JSON
{"foo": {"name": "bar"}}
JSON
,
'schema' => <<<JSON
{
"oneOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "object",
"properties": {
"name": {"enum":["baz"],"default":"baz"},
"meta": {"enum":["baz"],"default":"baz"}
}
}
}
},
{
"type": "object",
"properties": {
"foo": {
"type": "object",
"properties": {
"name": {"enum":["bar"],"default":"bar"},
"meta": {"enum":["bar"],"default":"bar"}
}
}
}
},
{
"type": "object",
"properties": {
"foo": {
"type": "object",
"properties": {
"name": {"enum":["zip"],"default":"zip"},
"meta": {"enum":["zip"],"default":"zip"}
}
}
}
}
]
}
JSON
,
'checkMode' => Constraint::CHECK_MODE_APPLY_DEFAULTS
],
'anyOf with apply defaults should not affect value passed to each sub schema (#711)' => [
'input' => <<<JSON
{