Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ it according to semantic versioning. For example, if your PR adds a breaking cha
should change the heading of the (upcoming) version to include a major version bump.

-->
# 5.24.11

## @rjsf/utils

- 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)

# 6.0.0-beta.2

## @rjsf/antd
Expand Down
64 changes: 64 additions & 0 deletions packages/core/test/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,70 @@ describeRepeated('Form common', (createFormComponent) => {
'root_any_of_field_second',
);

expect(node.querySelector(secondInputID).value).to.equal('changed!');
});
it('Should modify anyOf definition references when the defaults are set.', () => {
const schema = {
definitions: {
option1: {
properties: {
first: {
type: 'string',
},
},
},
option2: {
properties: {
second: {
type: 'string',
},
},
},
},
properties: {
any_of_field: {
anyOf: [
{
$ref: '#/definitions/option1',
},
{
$ref: '#/definitions/option2',
},
],
default: {
second: 'second!',
},
},
},
type: 'object',
};

const { node, onChange } = createFormComponent({
schema,
});

const secondInputID = '#root_any_of_field_second';
expect(node.querySelector(secondInputID).value).to.equal('second!');

act(() => {
fireEvent.change(node.querySelector(secondInputID), {
target: { value: 'changed!' },
});
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
any_of_field: {
second: 'changed!',
},
},
schema,
},
'root_any_of_field_second',
);

expect(node.querySelector(secondInputID).value).to.equal('changed!');
});
});
Expand Down
8 changes: 7 additions & 1 deletion packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
// For object defaults, only override parent defaults that are defined in
// schema.default.
defaults = mergeObjects(defaults!, schema.default as GenericObjectType) as T;
} else if (DEFAULT_KEY in schema && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY]) {
} else if (DEFAULT_KEY in schema && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY] && !schema[REF_KEY]) {
// If the schema has a default value, then we should use it as the default.
// And if the schema does not have anyOf or oneOf, this is done because we need to merge the defaults with the formData.
defaults = schema.default as unknown as T;
Expand All @@ -234,6 +234,12 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
updatedRecurseList = _recurseList.concat(refName!);
schemaToCompute = findSchemaDefinition<S>(refName, rootSchema);
}

// If the referenced schema exists and parentDefaults is not set
// Then set the defaults from the current schema for the referenced schema
if (schemaToCompute && !defaults) {
defaults = schema.default as T | undefined;
}
} else if (DEPENDENCIES_KEY in schema) {
// Get the default if set from properties to ensure the dependencies conditions are resolved based on it
const defaultFormData: T = {
Expand Down