Skip to content

fix: create deep copy before checking each sub schema in oneOf when only check_mode_apply_defaults is set #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
50 changes: 50 additions & 0 deletions tests/Constraints/UndefinedConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => <<<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
{
Expand Down