Skip to content

Commit

Permalink
- Fixed rjsf-team#2593 fully by also supporting data in formData in…
Browse files Browse the repository at this point in the history
… addition to data in `default` when dealing with `additionalProperties`
  • Loading branch information
heath-freenome committed Apr 4, 2023
1 parent 0ed4c78 commit acfe5e5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b

## @rjsf/utils
- Added protections against infinite recursion of `$ref`s for the `toIdSchema()`, `toPathSchema()` and `getDefaultFormState()` functions, fixing [#3560](https://github.com/rjsf-team/react-jsonschema-form/issues/3560)
- Updated `getDefaultFormState()` to handle object-based `additionalProperties` with defaults using `formData` in addition to values contained in a `default` object, fixing [#2593](https://github.com/rjsf-team/react-jsonschema-form/issues/2593)

# 5.5.0

Expand Down
47 changes: 26 additions & 21 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,32 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
maybeAddDefaultToObject<T>(acc, key, computedDefault, includeUndefinedValues, schema.required);
return acc;
}, {}) as T;
if (schema.additionalProperties && isObject(defaults)) {
const additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
Object.keys(defaults as GenericObjectType)
.filter((key) => !schema.properties || !schema.properties[key])
.forEach((key) => {
const computedDefault = computeDefaults(
validator,
additionalPropertiesSchema as S,
get(defaults, [key]),
rootSchema,
get(formData, [key]),
includeUndefinedValues === true,
_recurseList
);
maybeAddDefaultToObject<T>(
objectDefaults as GenericObjectType,
key,
computedDefault,
includeUndefinedValues
);
});
if (schema.additionalProperties) {
// as per spec additionalProperties may be either schema or boolean
const additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {};
const keys = new Set<string>();
if (isObject(defaults)) {
Object.keys(defaults as GenericObjectType)
.filter((key) => !schema.properties || !schema.properties[key])
.forEach((key) => keys.add(key));
}
if (isObject(formData)) {
Object.keys(formData as GenericObjectType)
.filter((key) => !schema.properties || !schema.properties[key])
.forEach((key) => keys.add(key));
}
keys.forEach((key) => {
const computedDefault = computeDefaults(
validator,
additionalPropertiesSchema as S,
get(defaults, [key]),
rootSchema,
get(formData, [key]),
includeUndefinedValues === true,
_recurseList
);
maybeAddDefaultToObject<T>(objectDefaults as GenericObjectType, key, computedDefault, includeUndefinedValues);
});
}
return objectDefaults;
}
Expand Down
39 changes: 39 additions & 0 deletions packages/utils/test/schema/getDefaultFormStateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,45 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
foo: 'bar',
});
});
it('test an object with additionalProperties type object with and formdata', () => {
const schema: RJSFSchema = {
type: 'object',
properties: {
test: {
title: 'Test',
type: 'object',
properties: {
foo: {
type: 'string',
},
},
additionalProperties: {
type: 'object',
properties: {
host: {
title: 'Host',
type: 'string',
default: 'localhost',
},
port: {
title: 'Port',
type: 'integer',
default: 389,
},
},
},
},
},
};
expect(computeDefaults(testValidator, schema, undefined, schema, { test: { foo: 'x', newKey: {} } })).toEqual({
test: {
newKey: {
host: 'localhost',
port: 389,
},
},
});
});
it('test computeDefaults handles an invalid property schema', () => {
const schema: RJSFSchema = {
type: 'object',
Expand Down

0 comments on commit acfe5e5

Please sign in to comment.