Skip to content

Commit

Permalink
Resolve schemas in combinator mappings
Browse files Browse the repository at this point in the history
Within the combinator mappings we try to determine the best fitting schema so that the
renderer can decide which tab to show. With the removal of the auto-resolving this no
longer worked in cases where the combinator contained references.

The situation is now improved by resolving references on the combinator level. This
should help in many use cases but still does not restore the old behavior completely.
Fully checking and resolving the whole sub schema is out of the scope with the new
simplified resolving architecture. If that is needed the user either needs use a custom
renderer or restore the old behavior by fully resolving their JSON Schema before handing
it over to JSON Forms, for example via json-refs.

This commit also removes the full resolving of the JSON Schema within the example
application which did mask this bug.
  • Loading branch information
LukasBoll authored Aug 19, 2022
1 parent e5b0dec commit 69441cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,9 @@ export const mapStateToCombinatorRendererProps = (
ownProps: OwnPropsOfControl,
keyword: CombinatorKeyword
): StatePropsOfCombinator => {
const { data, schema, ...props } = mapStateToControlProps(
const { data, schema, rootSchema, ...props } = mapStateToControlProps(
state,
ownProps
ownProps,
);

const ajv = state.jsonforms.core.ajv;
Expand All @@ -954,7 +954,12 @@ export const mapStateToCombinatorRendererProps = (
// element
for (let i = 0; i < schema[keyword]?.length; i++) {
try {
const valFn = ajv.compile(schema[keyword][i]);
let _schema = schema[keyword][i];
if(_schema.$ref){
_schema = Resolve.schema( rootSchema, _schema.$ref, rootSchema
);
}
const valFn = ajv.compile(_schema);
valFn(data);
if (dataIsValid(valFn.errors)) {
indexOfFittingSchema = i;
Expand All @@ -968,6 +973,7 @@ export const mapStateToCombinatorRendererProps = (
return {
data,
schema,
rootSchema,
...props,
indexOfFittingSchema,
uischemas: getUISchemas(state)
Expand Down
28 changes: 2 additions & 26 deletions packages/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
*/

import React, { useEffect, useMemo, useState } from 'react';
import { JsonForms, JsonFormsInitStateProps, JsonFormsReactProps } from '@jsonforms/react';
import { JsonForms, JsonFormsInitStateProps } from '@jsonforms/react';
import { ExampleDescription } from '@jsonforms/examples';
import {
JsonFormsCellRendererRegistryEntry,
JsonFormsRendererRegistryEntry,
JsonSchema
} from '@jsonforms/core';
import { resolveRefs } from 'json-refs';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import Highlight from 'react-highlight';
import 'highlight.js/styles/default.css';
Expand All @@ -48,28 +46,6 @@ type Action = {
apply: any
};

const ResolvedJsonForms = (
props: JsonFormsInitStateProps & JsonFormsReactProps
) => {
const [init, setInit] = useState(false);
const [schema, setSchema] = useState<JsonSchema>();
useEffect(() => {
if (!props.schema) {
setInit(true);
setSchema(props.schema);
} else {
resolveRefs(props.schema).then((result) => {
setInit(true);
setSchema(result.resolved);
});
}
}, [props.schema]);
if (!init) {
return null;
}
return <JsonForms {...props} schema={schema} />;
};

const getProps = (example: ExampleDescription, cells?: any, renderers?: any) => {
const schema = example.schema;
const uischema = example.uischema;
Expand Down Expand Up @@ -194,7 +170,7 @@ const App = ({ examples, cells, renderers}: AppProps) => {
))}
</div>
<div className='demo'>
<ResolvedJsonForms
<JsonForms
key={currentIndex}
{...props}
onChange={({ data }) => changeData(data)}
Expand Down
5 changes: 2 additions & 3 deletions packages/examples/src/examples/oneOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ export const uischema = {
const data = {
name: 'test',
addressOrUser: {
street_address: '1600 Pennsylvania Avenue NW',
city: 'Washington',
state: 'DC'
name: 'User',
mail: 'mail@example.com',
}
};

Expand Down

0 comments on commit 69441cd

Please sign in to comment.