diff --git a/CHANGELOG.md b/CHANGELOG.md index 043c85b1b2..d81f2f0eb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/packages/core/test/Form.test.jsx b/packages/core/test/Form.test.jsx index 8f4b8408b4..5f8709e3dc 100644 --- a/packages/core/test/Form.test.jsx +++ b/packages/core/test/Form.test.jsx @@ -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!'); }); }); diff --git a/packages/utils/src/schema/getDefaultFormState.ts b/packages/utils/src/schema/getDefaultFormState.ts index 1b82ef8b93..079f11992a 100644 --- a/packages/utils/src/schema/getDefaultFormState.ts +++ b/packages/utils/src/schema/getDefaultFormState.ts @@ -223,7 +223,7 @@ export function computeDefaults(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 = {