Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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.

-->

# 6.0.0-beta.2

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

- Updated the `description` field in field props to be a `string | ReactElement` and added `enableMarkdownInDescription` to the `GlobalUISchemaOptions` interface
- Support for bundled JSON Schemas [#4505](https://github.com/rjsf-team/react-jsonschema-form/issues/4505)
- 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)

## Dev / docs / playground

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