Skip to content

Commit

Permalink
Add support for 'then/else' paths
Browse files Browse the repository at this point in the history
Schema paths containing 'then' or 'else' were not transformed correctly to their data
paths. This is now fixed.

The JSON Forms resolver already allowed to omit 'oneOf', 'allOf' and 'anyOf' within UI
Schema scopes. The resolver will now do the same for 'then' and 'else' segments.

For this the resolving algorithm was rewritten to a depth first search. First trying to
resolve the paths as they were handed over and, if not successful, checking for omitted
segments.

Co-authored-by: Stefan Dirix <[email protected]>
  • Loading branch information
nmaier95 and sdirix authored May 12, 2022
1 parent 1410b58 commit 11de281
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 45 deletions.
7 changes: 3 additions & 4 deletions packages/core/src/util/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ export { compose as composePaths };
*/
export const toDataPathSegments = (schemaPath: string): string[] => {
const s = schemaPath
.replace(/anyOf\/[\d]\//g, '')
.replace(/allOf\/[\d]\//g, '')
.replace(/oneOf\/[\d]\//g, '');
.replace(/(anyOf|allOf|oneOf)\/[\d]\//g, '')
.replace(/(then|else)\//g, '');
const segments = s.split('/');

const decodedSegments = segments.map(decode);
Expand All @@ -79,7 +78,7 @@ export const toDataPathSegments = (schemaPath: string): string[] => {
*/
export const toDataPath = (schemaPath: string): string => {
return toDataPathSegments(schemaPath).join('.');
};
};

export const composeWithUi = (scopableUi: Scopable, path: string): string => {
const segments = toDataPathSegments(scopableUi.scope);
Expand Down
95 changes: 56 additions & 39 deletions packages/core/src/util/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import isEmpty from 'lodash/isEmpty';
import get from 'lodash/get';
import { JsonSchema } from '../models';
import { decode, encode } from './path';
import { JsonSchema, JsonSchema7 } from '../models';
import { decode } from './path';

/**
* Map for storing refs and the respective schemas they are pointing to.
Expand Down Expand Up @@ -112,50 +112,67 @@ export const resolveSchema = (
schema: JsonSchema,
schemaPath: string,
rootSchema: JsonSchema
): JsonSchema => {
const segments = schemaPath?.split('/').map(decode);
return resolveSchemaWithSegments(schema, segments, rootSchema);
};

const resolveSchemaWithSegments = (
schema: JsonSchema,
pathSegments: string[],
rootSchema: JsonSchema
): JsonSchema => {
if (isEmpty(schema)) {
return undefined;
}
const validPathSegments = schemaPath.split('/').map(decode);
let resultSchema = schema;
for (let i = 0; i < validPathSegments.length; i++) {
let pathSegment = validPathSegments[i];
resultSchema =
resultSchema === undefined || resultSchema.$ref === undefined
? resultSchema
// use rootSchema as value for schema, since schema is undefined or a ref
: resolveSchema(rootSchema, resultSchema.$ref, rootSchema);
if (invalidSegment(pathSegment)) {
// skip invalid segments
continue;
}
let curSchema = get(resultSchema, pathSegment);
if (!curSchema) {
// resolving was not successful, check whether the scope omitted an oneOf, allOf or anyOf and resolve anyway
const schemas = [].concat(
resultSchema?.oneOf ?? [],
resultSchema?.allOf ?? [],
resultSchema?.anyOf ?? []

if (schema.$ref) {
schema = resolveSchema(rootSchema, schema.$ref, rootSchema);
}

if (!pathSegments || pathSegments.length === 0) {
return schema;
}

const [segment, ...remainingSegments] = pathSegments;

if (invalidSegment(segment)) {
return resolveSchemaWithSegments(schema, remainingSegments, rootSchema);
}

const singleSegmentResolveSchema = get(schema, segment);

const resolvedSchema = resolveSchemaWithSegments(singleSegmentResolveSchema, remainingSegments, rootSchema);
if (resolvedSchema) {
return resolvedSchema;
}

if (segment === 'properties' || segment === 'items') {
// Let's try to resolve the path, assuming oneOf/allOf/anyOf/then/else was omitted.
// We only do this when traversing an object or array as we want to avoid
// following a property which is named oneOf, allOf, anyOf, then or else.
let alternativeResolveResult = undefined;

const subSchemas = [].concat(
schema.oneOf ?? [],
schema.allOf ?? [],
schema.anyOf ?? [],
(schema as JsonSchema7).then ?? [],
(schema as JsonSchema7).else ?? []
);

for (const subSchema of subSchemas) {
alternativeResolveResult = resolveSchemaWithSegments(
subSchema,
[segment, ...remainingSegments],
rootSchema
);
for (let item of schemas) {
curSchema = resolveSchema(item, validPathSegments.slice(i).map(encode).join('/'), rootSchema);
if (curSchema) {
break;
}
}
if (curSchema) {
// already resolved rest of the path
resultSchema = curSchema;
if (alternativeResolveResult) {
break;
}
}
resultSchema = curSchema;
return alternativeResolveResult;
}

if (resultSchema !== undefined && resultSchema.$ref !== undefined) {
return resolveSchema(rootSchema, resultSchema.$ref, rootSchema)
?? schema;
}

return resultSchema;
};
return undefined;
}
27 changes: 27 additions & 0 deletions packages/core/test/util/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,39 @@ test('toDataPath ', t => {
test('toDataPath replace anyOf', t => {
t.is(toDataPath('/anyOf/1/properties/foo/anyOf/1/properties/bar'), 'foo.bar');
});
test('toDataPath replace anyOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/anyOf/1/then/properties/foo'), 'foo');
});
test('toDataPath replace multiple directly nested anyOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/anyOf/1/then/anyOf/0/then/properties/foo'), 'foo');
});
test('toDataPath replace multiple nested properties with anyOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/anyOf/1/properties/foo/anyOf/0/then/properties/bar'), 'foo.bar');
});
test('toDataPath replace allOf', t => {
t.is(toDataPath('/allOf/1/properties/foo/allOf/1/properties/bar'), 'foo.bar');
});
test('toDataPath replace allOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/allOf/1/then/properties/foo'), 'foo');
});
test('toDataPath replace multiple directly nested allOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/allOf/1/then/allOf/0/then/properties/foo'), 'foo');
});
test('toDataPath replace multiple nested properties with allOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/allOf/1/properties/foo/allOf/0/then/properties/bar'), 'foo.bar');
});
test('toDataPath replace oneOf', t => {
t.is(toDataPath('/oneOf/1/properties/foo/oneOf/1/properties/bar'), 'foo.bar');
});
test('toDataPath replace oneOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/oneOf/1/then/properties/foo'), 'foo');
});
test('toDataPath replace multiple directly nested oneOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/oneOf/1/then/oneOf/0/then/properties/foo'), 'foo');
});
test('toDataPath replace multiple nested properties with oneOf in combination with conditional schema compositions', t => {
t.is(toDataPath('/oneOf/1/properties/foo/oneOf/0/then/properties/bar'), 'foo.bar');
});
test('toDataPath replace all combinators', t => {
t.is(
toDataPath(
Expand Down
45 changes: 44 additions & 1 deletion packages/core/test/util/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,58 @@ test('resolveSchema - resolves schema with any ', t => {
}
}]
}
}
},
anyOf: [
{
if: {
properties: {
exist: {
const: true
}
}
},
then: {
properties: {
lastname: {
type: 'string'
}
}
},
else: {
properties: {
firstname: {
type: 'string'
},
address: {
type: 'object',
anyOf: [
{
properties: {
street: {
type: 'string'
}
}
}
]
}
}
}
}
]
};
// test backward compatibility
t.deepEqual(resolveSchema(schema, '#/properties/description/oneOf/0/properties/name', schema), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/properties/description/oneOf/1/properties/index', schema), {type: 'number'});
t.deepEqual(resolveSchema(schema, '#/anyOf/0/then/properties/lastname', schema), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/anyOf/0/else/properties/firstname', schema), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/anyOf/0/else/properties/address/anyOf/0/properties/street', schema), {type: 'string'});
// new simple approach
t.deepEqual(resolveSchema(schema, '#/properties/description/properties/name', schema), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/properties/description/properties/index', schema), {type: 'number'});
t.deepEqual(resolveSchema(schema, '#/properties/description/properties/exist', schema), {type: 'boolean'});
t.deepEqual(resolveSchema(schema, '#/properties/lastname', schema), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/properties/firstname', schema), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/properties/address/properties/street', schema), {type: 'string'});
t.is(resolveSchema(schema, '#/properties/description/properties/notfound', schema), undefined);
// refs
t.deepEqual(resolveSchema(schema, '#/properties/description/properties/element/properties/geometry', schema), {type: 'string'});
Expand Down
118 changes: 118 additions & 0 deletions packages/examples/src/examples/conditional-schema-compositions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { registerExamples } from '../register';

export const schema = {
type: 'object',
properties: {
name: {
type: 'string',
minLength: 1,
description: 'The task\'s name',
},
recurrence: {
type: 'string',
enum: ['Never', 'Daily', 'Weekly', 'Monthly'],
},
},
anyOf: [
{
if: {
properties: {
recurrence: {
const: 'Never'
}
}
},
then: {
properties: {
lastname: {
type: 'string'
},
age: {
type: 'number'
}
}
}
},
]
};

export const uischema = {
type: 'HorizontalLayout',
elements: [
{
type: 'VerticalLayout',
elements: [
{
type: 'Control',
scope: '#/properties/name',
},
{
type: 'Control',
scope: '#/properties/recurrence',
},
{
type: 'Control',
scope: '#/anyOf/0/then/properties/lastname',
rule: {
effect: 'SHOW',
condition: {
scope: '#/properties/recurrence',
schema: {
const: 'Never'
}
}
}
},
{
type: 'Control',
scope: '#/properties/age',
rule: {
effect: 'SHOW',
condition: {
scope: '#/properties/recurrence',
schema: {
const: 'Never'
}
}
}
},
],
},
],
};

const data = {};

registerExamples([
{
name: 'conditional-schema-compositions',
label: 'Conditional Schema Compositions',
data,
schema,
uischema
}
]);
4 changes: 3 additions & 1 deletion packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import * as enumInArray from './examples/enumInArray';
import * as readonly from './examples/readonly';
import * as bug_1779 from './examples/1779';
import * as bug_1645 from './examples/1645';
import * as conditionalSchemaComposition from './examples/conditional-schema-compositions';
export * from './register';
export * from './example';

Expand Down Expand Up @@ -134,5 +135,6 @@ export {
enumInArray,
readonly,
bug_1779,
bug_1645
bug_1645,
conditionalSchemaComposition
};

0 comments on commit 11de281

Please sign in to comment.