Skip to content

Commit a94f60c

Browse files
fix: create deep copy before checking each sub schema in oneOf when only check_mode_apply_defaults is set (#795)
Fixes #510
1 parent 649793d commit a94f60c

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Create deep copy before checking each sub schema in oneOf ([#791](https://github.com/jsonrainbow/json-schema/pull/791))
1616
- Create deep copy before checking each sub schema in anyOf ([#792](https://github.com/jsonrainbow/json-schema/pull/792))
1717
- Correctly set the schema ID when passing it as assoc array ([#794](https://github.com/jsonrainbow/json-schema/pull/794))
18+
- 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))
1819

1920
### Changed
2021
- Used PHPStan's int-mask-of<T> type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,13 @@ protected function validateOfProperties(&$value, $schema, JsonPointer $path, $i
359359
$allErrors = [];
360360
$matchedSchemas = [];
361361
$startErrors = $this->getErrors();
362-
$coerce = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES);
362+
$coerceOrDefaults = $this->factory->getConfig(self::CHECK_MODE_COERCE_TYPES | self::CHECK_MODE_APPLY_DEFAULTS);
363363

364364
foreach ($schema->oneOf as $oneOf) {
365365
try {
366366
$this->errors = [];
367367

368-
$oneOfValue = $coerce ? DeepCopy::copyOf($value) : $value;
368+
$oneOfValue = $coerceOrDefaults ? DeepCopy::copyOf($value) : $value;
369369
$this->checkUndefined($oneOfValue, $oneOf, $path, $i);
370370
if (count($this->getErrors()) === 0) {
371371
$matchedSchemas[] = ['schema' => $oneOf, 'value' => $oneOfValue];

tests/Constraints/UndefinedConstraintTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,56 @@ public function getValidTests(): array
7070
,
7171
'checkMode' => Constraint::CHECK_MODE_COERCE_TYPES
7272
],
73+
'oneOf with apply defaults should not affect value passed to each sub schema (#510)' => [
74+
'input' => <<<JSON
75+
{"foo": {"name": "bar"}}
76+
JSON
77+
,
78+
'schema' => <<<JSON
79+
{
80+
"oneOf": [
81+
{
82+
"type": "object",
83+
"properties": {
84+
"foo": {
85+
"type": "object",
86+
"properties": {
87+
"name": {"enum":["baz"],"default":"baz"},
88+
"meta": {"enum":["baz"],"default":"baz"}
89+
}
90+
}
91+
}
92+
},
93+
{
94+
"type": "object",
95+
"properties": {
96+
"foo": {
97+
"type": "object",
98+
"properties": {
99+
"name": {"enum":["bar"],"default":"bar"},
100+
"meta": {"enum":["bar"],"default":"bar"}
101+
}
102+
}
103+
}
104+
},
105+
{
106+
"type": "object",
107+
"properties": {
108+
"foo": {
109+
"type": "object",
110+
"properties": {
111+
"name": {"enum":["zip"],"default":"zip"},
112+
"meta": {"enum":["zip"],"default":"zip"}
113+
}
114+
}
115+
}
116+
}
117+
]
118+
}
119+
JSON
120+
,
121+
'checkMode' => Constraint::CHECK_MODE_APPLY_DEFAULTS
122+
],
73123
'anyOf with apply defaults should not affect value passed to each sub schema (#711)' => [
74124
'input' => <<<JSON
75125
{

0 commit comments

Comments
 (0)