diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b9a7e69a..f2ad163081 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,10 +23,15 @@ should change the heading of the (upcoming) version to include a major version b ## @rjsf/material-ui - Resolve the React error caused by the propagation of the `hideError` property to the DOM element, fixing [#3945](https://github.com/rjsf-team/react-jsonschema-form/issues/3945) +## @rjsf/utils + +- Update `sanitizeDataForNewSchema()` to avoid spreading strings and Arrays into the returned value when the old schema is of type `string` or `array` and the new schema is of type `object`. Fixing [#3922](https://github.com/rjsf-team/react-jsonschema-form/issues/3922) + # 5.14.1 ## @rjsf/utils +- Update `sanitizeDataForNewSchema()` to avoid spreading strings and Arrays into the returned value when the old schema is of type `string` or `array` and the new schema is of type `object`. Fixing [#3922](https://github.com/rjsf-team/react-jsonschema-form/issues/3922) - update types for `labelValue` to have more granular return types, fixing [#3946](https://github.com/rjsf-team/react-jsonschema-form/issues/3946) ## Dev / playground diff --git a/packages/utils/src/schema/sanitizeDataForNewSchema.ts b/packages/utils/src/schema/sanitizeDataForNewSchema.ts index 2ecba355e2..cd07eea930 100644 --- a/packages/utils/src/schema/sanitizeDataForNewSchema.ts +++ b/packages/utils/src/schema/sanitizeDataForNewSchema.ts @@ -137,7 +137,7 @@ export default function sanitizeDataForNewSchema< }); newFormData = { - ...data, + ...(typeof data == 'string' || Array.isArray(data) ? undefined : data), ...removeOldSchemaData, ...nestedData, }; diff --git a/packages/utils/test/schema/sanitizeDataForNewSchemaTest.ts b/packages/utils/test/schema/sanitizeDataForNewSchemaTest.ts index a6d85d965b..1069ee9780 100644 --- a/packages/utils/test/schema/sanitizeDataForNewSchemaTest.ts +++ b/packages/utils/test/schema/sanitizeDataForNewSchemaTest.ts @@ -456,5 +456,32 @@ export default function sanitizeDataForNewSchemaTest(testValidator: TestValidato const formData = { foo: '1' }; expect(schemaUtils.sanitizeDataForNewSchema(newSchema, oldSchema, formData)).toEqual(formData); }); + it('returns empty object when the old schema is of type string and the new contains "property" field', () => { + const oldSchema: RJSFSchema = { type: 'string' }; + const newSchema: RJSFSchema = { + properties: { + foo: { + type: 'string', + }, + }, + }; + expect(schemaUtils.sanitizeDataForNewSchema(newSchema, oldSchema, 'qwerty')).toEqual({}); + }); + it('returns empty object when the old schema is of type array and the new contains "property" field', () => { + const oldSchema: RJSFSchema = { + type: 'array', + items: { + type: 'string', + }, + }; + const newSchema: RJSFSchema = { + properties: { + foo: { + type: 'string', + }, + }, + }; + expect(schemaUtils.sanitizeDataForNewSchema(newSchema, oldSchema, ['qwerty', 'asdfg'])).toEqual({}); + }); }); }