Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations to improve performance on complex conditional schemas #2466

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
eef551d
Implementation of If Then Else
Saulzi Feb 27, 2020
51387a0
Fix playground example
nickgros Jul 12, 2021
fca3592
Fix resolveSchema to handle more cases
nickgros Jul 12, 2021
ba15fd3
Don't remove the root schema after validating
nickgros Jul 12, 2021
75665ca
Warn when encountering error in validation
nickgros Jul 12, 2021
23288ae
Add tests to cover error cases
nickgros Jul 12, 2021
4cf5f7f
Merge branch 'master' of https://github.com/rjsf-team/react-jsonschem…
nickgros Jul 12, 2021
78fc62c
Memoize withIdRefPrefix and prevent recompiling during form validation
nickgros Jul 15, 2021
58d3335
Fix tests
nickgros Jul 15, 2021
5e7a08d
Memoize all the things
nickgros Jul 16, 2021
667903b
Import only memoize from lodash
nickgros Jul 22, 2021
ab36d1e
Revert outdated docs reorganization
nickgros Jul 22, 2021
b00f49f
Revert `const` changes
nickgros Jul 22, 2021
833006a
Remove default value from enum example
nickgros Jul 22, 2021
8796c9d
Delete `allOf` rather than set to undefined. Fixes liveOmit issue.
nickgros Jul 22, 2021
b6c53c8
Merge branch 'master' of https://github.com/rjsf-team/react-jsonschem…
nickgros Jul 22, 2021
f72a62c
Added tests
Juansasa Aug 6, 2021
75c4d84
Changed resolve method's name
Juansasa Aug 9, 2021
a08c563
Added $ref tests
Juansasa Aug 13, 2021
07a43c8
Merge branch 'master' into conditional-support
nickgros Sep 1, 2021
52747fb
Merge branch 'master' of https://github.com/rjsf-team/react-jsonschem…
nickgros Feb 10, 2022
123c480
Merge branch 'conditional-support' of github.com:nickgros/react-jsons…
nickgros Feb 10, 2022
964f537
Merge branch 'if_then_else' of https://github.com/stakater/react-json…
nickgros Feb 10, 2022
544133b
Merge branch 'master' of https://github.com/rjsf-team/react-jsonschem…
nickgros Feb 20, 2022
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: 1 addition & 1 deletion packages/core/src/components/fields/SchemaField.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ function SchemaFieldRender(props) {
uiSchema["ui:FieldTemplate"] || registry.FieldTemplate || DefaultTemplate;
let idSchema = props.idSchema;
const schema = retrieveSchema(props.schema, rootSchema, formData);

idSchema = mergeObjects(
toIdSchema(schema, null, rootSchema, formData, idPrefix, idSeparator),
idSchema
Expand Down Expand Up @@ -404,7 +405,6 @@ function SchemaFieldRender(props) {
</FieldTemplate>
);
}

class SchemaField extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return !deepEquals(this.props, nextProps);
Expand Down
66 changes: 50 additions & 16 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import jsonpointer from "jsonpointer";
import fields from "./components/fields";
import widgets from "./components/widgets";
import validateFormData, { isValid } from "./validate";
import memoize from "lodash/memoize";

// Use the same default object to optimize memoized functions that rely on referential equality
const DEFAULT_ROOT_SCHEMA = {};
const DEFAULT_FORM_DATA = {};
export const ADDITIONAL_PROPERTY_FLAG = "__additional_property";

const widgetMap = {
Expand Down Expand Up @@ -171,11 +175,14 @@ export function hasWidget(schema, widget, registeredWidgets = {}) {
}
}

function computeDefaults(
const cacheKeyFn = (...args) => args.map(arg => JSON.stringify(arg)).join("_");
const computeDefaults = memoize(_computeDefaults, cacheKeyFn);

function _computeDefaults(
_schema,
parentDefaults,
rootSchema,
rawFormData = {},
rawFormData = DEFAULT_FORM_DATA,
includeUndefinedValues = false
) {
let schema = isObject(_schema) ? _schema : {};
Expand All @@ -189,6 +196,8 @@ function computeDefaults(
} else if ("default" in schema) {
// Use schema defaults for this node.
defaults = schema.default;
} else if ("const" in schema) {
defaults = schema.const;
} else if ("$ref" in schema) {
// Use referenced schema defaults for this node.
const refSchema = findSchemaDefinition(schema.$ref, rootSchema);
Expand Down Expand Up @@ -301,7 +310,7 @@ function computeDefaults(
export function getDefaultFormState(
_schema,
formData,
rootSchema = {},
rootSchema = DEFAULT_ROOT_SCHEMA,
includeUndefinedValues = false
) {
if (!isObject(_schema)) {
Expand Down Expand Up @@ -522,7 +531,7 @@ export function toConstant(schema) {
}
}

export function isSelect(_schema, rootSchema = {}) {
export function isSelect(_schema, rootSchema = DEFAULT_ROOT_SCHEMA) {
const schema = retrieveSchema(_schema, rootSchema);
const altSchemas = schema.oneOf || schema.anyOf;
if (Array.isArray(schema.enum)) {
Expand All @@ -533,14 +542,18 @@ export function isSelect(_schema, rootSchema = {}) {
return false;
}

export function isMultiSelect(schema, rootSchema = {}) {
export function isMultiSelect(schema, rootSchema = DEFAULT_ROOT_SCHEMA) {
if (!schema.uniqueItems || !schema.items) {
return false;
}
return isSelect(schema.items, rootSchema);
}

export function isFilesArray(schema, uiSchema, rootSchema = {}) {
export function isFilesArray(
schema,
uiSchema,
rootSchema = DEFAULT_ROOT_SCHEMA
) {
if (uiSchema["ui:widget"] === "files") {
return true;
} else if (schema.items) {
Expand Down Expand Up @@ -594,7 +607,7 @@ export function optionsList(schema) {
}
}

export function findSchemaDefinition($ref, rootSchema = {}) {
export function findSchemaDefinition($ref, rootSchema = DEFAULT_ROOT_SCHEMA) {
const origRef = $ref;
if ($ref.startsWith("#")) {
// Decode URI fragment representation.
Expand Down Expand Up @@ -635,8 +648,8 @@ export const guessType = function guessType(value) {
// This function will create new "properties" items for each key in our formData
export function stubExistingAdditionalProperties(
schema,
rootSchema = {},
formData = {}
rootSchema = DEFAULT_ROOT_SCHEMA,
formData = DEFAULT_FORM_DATA
) {
// Clone the schema so we don't ruin the consumer's original
schema = {
Expand Down Expand Up @@ -704,12 +717,18 @@ const resolveCondition = (schema, rootSchema, formData) => {
}
};

export const resolveSchema = memoize(_resolveSchema, cacheKeyFn);

/**
* Resolves references and dependencies within a schema and its 'allOf' children.
*
* Called internally by retrieveSchema.
*/
export function resolveSchema(schema, rootSchema = {}, formData = {}) {
export function _resolveSchema(
schema,
rootSchema = DEFAULT_ROOT_SCHEMA,
formData = DEFAULT_FORM_DATA
) {
if (schema.hasOwnProperty("$ref")) {
return resolveReference(schema, rootSchema, formData);
} else if (schema.hasOwnProperty("dependencies")) {
Expand All @@ -723,7 +742,8 @@ export function resolveSchema(schema, rootSchema = {}, formData = {}) {
),
};
} else {
// No $ref or dependencies attribute found, returning the original schema.
// No $ref, dependencies, or allOf attribute found, so there's nothing to resolve.
// Returning the original schema.
return schema;
}
}
Expand All @@ -741,9 +761,13 @@ function resolveReference(schema, rootSchema, formData) {
);
}

export function retrieveSchema(schema, rootSchema = {}, formData = {}) {
export function _retrieveSchema(
schema,
rootSchema = DEFAULT_ROOT_SCHEMA,
formData = DEFAULT_FORM_DATA
) {
if (!isObject(schema)) {
return {};
return DEFAULT_ROOT_SCHEMA;
}
let resolvedSchema = resolveSchema(schema, rootSchema, formData);

Expand All @@ -763,19 +787,24 @@ export function retrieveSchema(schema, rootSchema = {}, formData = {}) {
return resolvedSchemaWithoutAllOf;
}
}

const hasAdditionalProperties =
resolvedSchema.hasOwnProperty("additionalProperties") &&
resolvedSchema.additionalProperties !== false;

if (hasAdditionalProperties) {
return stubExistingAdditionalProperties(
resolvedSchema = stubExistingAdditionalProperties(
resolvedSchema,
rootSchema,
formData
);
}

return resolvedSchema;
}

export const retrieveSchema = memoize(_retrieveSchema, cacheKeyFn);

function resolveDependencies(schema, rootSchema, formData) {
// Drop the dependencies from the source schema.
let { dependencies = {}, ...resolvedSchema } = schema;
Expand Down Expand Up @@ -1046,7 +1075,7 @@ export function toIdSchema(
schema,
id,
rootSchema,
formData = {},
formData = DEFAULT_FORM_DATA,
idPrefix = "root",
idSeparator = "_"
) {
Expand Down Expand Up @@ -1087,7 +1116,12 @@ export function toIdSchema(
return idSchema;
}

export function toPathSchema(schema, name = "", rootSchema, formData = {}) {
export function toPathSchema(
schema,
name = "",
rootSchema,
formData = DEFAULT_FORM_DATA
) {
const pathSchema = {
$name: name.replace(/^\./, ""),
};
Expand Down
Loading