Skip to content

Commit 70db248

Browse files
Bug: schema references in combinators(allOf, anyOf, oneOf) cannot be modified when defaults were set (rjsf-team#4600)
* fixed issue with schema references in combinators * update changelog --------- Co-authored-by: Heath C <[email protected]>
1 parent e6cbaf7 commit 70db248

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
1515
should change the heading of the (upcoming) version to include a major version bump.
1616
1717
-->
18+
1819
# 6.0.0-beta.2
1920

2021
## @rjsf/antd
@@ -62,6 +63,7 @@ should change the heading of the (upcoming) version to include a major version b
6263

6364
- Updated the `description` field in field props to be a `string | ReactElement` and added `enableMarkdownInDescription` to the `GlobalUISchemaOptions` interface
6465
- Support for bundled JSON Schemas [#4505](https://github.com/rjsf-team/react-jsonschema-form/issues/4505)
66+
- Fixed issue with schema references in combinators(allOf, anyOf, oneOf) could not be modified when defaults were set, fixing [#4555](https://github.com/rjsf-team/react-jsonschema-form/issues/4555)
6567

6668
## Dev / docs / playground
6769

packages/core/test/Form.test.jsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,70 @@ describeRepeated('Form common', (createFormComponent) => {
14271427
'root_any_of_field_second',
14281428
);
14291429

1430+
expect(node.querySelector(secondInputID).value).to.equal('changed!');
1431+
});
1432+
it('Should modify anyOf definition references when the defaults are set.', () => {
1433+
const schema = {
1434+
definitions: {
1435+
option1: {
1436+
properties: {
1437+
first: {
1438+
type: 'string',
1439+
},
1440+
},
1441+
},
1442+
option2: {
1443+
properties: {
1444+
second: {
1445+
type: 'string',
1446+
},
1447+
},
1448+
},
1449+
},
1450+
properties: {
1451+
any_of_field: {
1452+
anyOf: [
1453+
{
1454+
$ref: '#/definitions/option1',
1455+
},
1456+
{
1457+
$ref: '#/definitions/option2',
1458+
},
1459+
],
1460+
default: {
1461+
second: 'second!',
1462+
},
1463+
},
1464+
},
1465+
type: 'object',
1466+
};
1467+
1468+
const { node, onChange } = createFormComponent({
1469+
schema,
1470+
});
1471+
1472+
const secondInputID = '#root_any_of_field_second';
1473+
expect(node.querySelector(secondInputID).value).to.equal('second!');
1474+
1475+
act(() => {
1476+
fireEvent.change(node.querySelector(secondInputID), {
1477+
target: { value: 'changed!' },
1478+
});
1479+
});
1480+
1481+
sinon.assert.calledWithMatch(
1482+
onChange.lastCall,
1483+
{
1484+
formData: {
1485+
any_of_field: {
1486+
second: 'changed!',
1487+
},
1488+
},
1489+
schema,
1490+
},
1491+
'root_any_of_field_second',
1492+
);
1493+
14301494
expect(node.querySelector(secondInputID).value).to.equal('changed!');
14311495
});
14321496
});

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
223223
// For object defaults, only override parent defaults that are defined in
224224
// schema.default.
225225
defaults = mergeObjects(defaults!, schema.default as GenericObjectType) as T;
226-
} else if (DEFAULT_KEY in schema && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY]) {
226+
} else if (DEFAULT_KEY in schema && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY] && !schema[REF_KEY]) {
227227
// If the schema has a default value, then we should use it as the default.
228228
// And if the schema does not have anyOf or oneOf, this is done because we need to merge the defaults with the formData.
229229
defaults = schema.default as unknown as T;
@@ -234,6 +234,12 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
234234
updatedRecurseList = _recurseList.concat(refName!);
235235
schemaToCompute = findSchemaDefinition<S>(refName, rootSchema);
236236
}
237+
238+
// If the referenced schema exists and parentDefaults is not set
239+
// Then set the defaults from the current schema for the referenced schema
240+
if (schemaToCompute && !defaults) {
241+
defaults = schema.default as T | undefined;
242+
}
237243
} else if (DEPENDENCIES_KEY in schema) {
238244
// Get the default if set from properties to ensure the dependencies conditions are resolved based on it
239245
const defaultFormData: T = {

0 commit comments

Comments
 (0)