Skip to content

Commit 06e85ca

Browse files
authored
Bug: schema references could not be modified when defaults were set (#4626)
* Fixed issue where oneOf schema references could not be modified when defaults were set * fixed changelog
1 parent c967820 commit 06e85ca

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ should change the heading of the (upcoming) version to include a major version b
1818

1919
# 6.0.0-beta.8
2020

21-
## @rjsf/util
22-
23-
- Fixed form data propagation with `patternProperties` [#4617](https://github.com/rjsf-team/react-jsonschema-form/pull/4617)
24-
2521
## @rjsf/chakra-ui
2622

2723
- Added `getChakra` to package exports
2824
- Restored the `ui:options` customization
2925

26+
## @rjsf/util
27+
28+
- Fixed form data propagation with `patternProperties` [#4617](https://github.com/rjsf-team/react-jsonschema-form/pull/4617)
29+
- Fixed issue where oneOf schema references could not be modified when defaults were set, fixing [#4580](https://github.com/rjsf-team/react-jsonschema-form/issues/4580).
30+
3031
## Dev / docs / playground
3132

3233
- Updated precompiled schemas documentation in `validation.md` based on v6 changes, addressingg [#4618](https://github.com/rjsf-team/react-jsonschema-form/issues/4618)

packages/core/test/Form.test.jsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,60 @@ describeRepeated('Form common', (createFormComponent) => {
14931493

14941494
expect(node.querySelector(secondInputID).value).to.equal('changed!');
14951495
});
1496+
it('Should modify oneOf object with references when the defaults are set.', () => {
1497+
const schema = {
1498+
type: 'object',
1499+
$defs: {
1500+
protocol: {
1501+
type: 'string',
1502+
enum: ['fast', 'balanced', 'stringent'],
1503+
default: 'fast',
1504+
},
1505+
},
1506+
oneOf: [
1507+
{
1508+
properties: {
1509+
protocol: {
1510+
$ref: '#/$defs/protocol',
1511+
},
1512+
},
1513+
},
1514+
{
1515+
properties: {
1516+
something: {
1517+
type: 'number',
1518+
},
1519+
},
1520+
},
1521+
],
1522+
};
1523+
1524+
const { node, onChange } = createFormComponent({
1525+
schema,
1526+
});
1527+
1528+
const protocolInputID = '#root_protocol';
1529+
expect(node.querySelector(protocolInputID).value).to.equal('0');
1530+
1531+
act(() => {
1532+
fireEvent.change(node.querySelector(protocolInputID), {
1533+
target: { value: '1' },
1534+
});
1535+
});
1536+
1537+
sinon.assert.calledWithMatch(
1538+
onChange.lastCall,
1539+
{
1540+
formData: {
1541+
protocol: 'balanced',
1542+
},
1543+
schema,
1544+
},
1545+
'root_protocol',
1546+
);
1547+
1548+
expect(node.querySelector(protocolInputID).value).to.equal('1');
1549+
});
14961550
});
14971551

14981552
describe('Blur handler', () => {

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,14 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
204204
required,
205205
shouldMergeDefaultsIntoFormData = false,
206206
} = computeDefaultsProps;
207-
const formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
207+
let formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
208208
const schema: S = isObject(rawSchema) ? rawSchema : ({} as S);
209209
// Compute the defaults recursively: give highest priority to deepest nodes.
210210
let defaults: T | T[] | undefined = parentDefaults;
211211
// If we get a new schema, then we need to recompute defaults again for the new schema found.
212212
let schemaToCompute: S | null = null;
213213
let experimental_dfsb_to_compute = experimental_defaultFormStateBehavior;
214214
let updatedRecurseList = _recurseList;
215-
216215
if (
217216
schema[CONST_KEY] &&
218217
experimental_defaultFormStateBehavior?.constAsDefaults !== 'never' &&
@@ -240,6 +239,13 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
240239
if (schemaToCompute && !defaults) {
241240
defaults = schema.default as T | undefined;
242241
}
242+
243+
// If shouldMergeDefaultsIntoFormData is true
244+
// And the schemaToCompute is set and the rawFormData is not an object
245+
// Then set the formData to the rawFormData
246+
if (shouldMergeDefaultsIntoFormData && schemaToCompute && !isObject(rawFormData)) {
247+
formData = rawFormData as T;
248+
}
243249
} else if (DEPENDENCIES_KEY in schema) {
244250
// Get the default if set from properties to ensure the dependencies conditions are resolved based on it
245251
const defaultFormData: T = {

0 commit comments

Comments
 (0)