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

Added auto anyOf/oneOf/allOf resolving #1702

Merged
merged 4 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
60 changes: 41 additions & 19 deletions packages/core/src/util/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
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
Expand Down Expand Up @@ -57,10 +57,7 @@ export const resolveData = (instance: any, dataPath: string): any => {
return dataPathSegments
.map(segment => decodeURIComponent(segment))
.reduce((curInstance, decodedSegment) => {
if (
!curInstance ||
!curInstance.hasOwnProperty(decodedSegment)
) {
if (!curInstance || !curInstance.hasOwnProperty(decodedSegment)) {
return undefined;
}

Expand Down Expand Up @@ -107,6 +104,9 @@ export const findAllRefs = (
return result;
};

const invalidSegment = (pathSegment: string) =>
pathSegment === '#' || pathSegment === undefined || pathSegment === '';

/**
* Resolve the given schema path in order to obtain a subschema.
* @param {JsonSchema} schema the root schema from which to start
Expand All @@ -123,17 +123,39 @@ export const resolveSchema = (
return undefined;
}
const validPathSegments = schemaPath.split('/');
const invalidSegment = (pathSegment: string) =>
pathSegment === '#' || pathSegment === undefined || pathSegment === '';
const resultSchema = validPathSegments.reduce((curSchema, pathSegment) => {
curSchema =
curSchema === undefined || curSchema.$ref === undefined
? curSchema
: resolveSchema(schema, curSchema.$ref);
return invalidSegment(pathSegment)
? curSchema
: get(curSchema, pathSegment);
}, schema);
let resultSchema = schema;
for (let i = 0; i < validPathSegments.length; i++) {
let pathSegment = validPathSegments[i];
resultSchema =
resultSchema === undefined || resultSchema.$ref === undefined
? resultSchema
: resolveSchema(schema, resultSchema.$ref);
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 ?? []
);
for (let item of schemas) {
curSchema = resolveSchema(item, validPathSegments.slice(i).join('/'));
if (curSchema) {
break;
}
}
if (curSchema) {
// already resolved rest of the path
resultSchema = curSchema;
break;
}
}
resultSchema = curSchema;
}
// TODO: because schema is already scoped we might end up with refs pointing
// outside of the current schema. It would be better if we'd always could deal
// with absolute paths here, so that we don't need to keep two different
Expand Down
51 changes: 46 additions & 5 deletions packages/core/test/util/resolvers.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
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
Expand All @@ -22,7 +22,7 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { findRefs } from '../../src/util/resolvers';
import { findRefs, resolveSchema } from '../../src/util/resolvers';
import test from 'ava';

test('findRef - does not fail on empty input object ', t => {
Expand Down Expand Up @@ -81,3 +81,44 @@ test('findRef - no ref in no ref object ', t => {
};
t.true(Object.keys(findRefs(refObject)).length === 0);
});


test('resolveSchema - resolves schema with any ', t => {
const schema = {
type: 'object',
properties: {
description: {
oneOf: [{
type: 'object',
properties: {
name: {
type: 'string'
}
}
}, {
type: 'object',
properties: {
index: {
type: 'number'
}
}
}, {
type: 'object',
properties: {
exist: {
type: 'boolean'
}
}
}]
}
}
};
// test backward compatibility
t.deepEqual(resolveSchema(schema, '#/properties/description/oneOf/0/properties/name'), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/properties/description/oneOf/1/properties/index'), {type: 'number'});
// new simple approach
t.deepEqual(resolveSchema(schema, '#/properties/description/properties/name'), {type: 'string'});
t.deepEqual(resolveSchema(schema, '#/properties/description/properties/index'), {type: 'number'});
t.deepEqual(resolveSchema(schema, '#/properties/description/properties/exist'), {type: 'boolean'});
t.is(resolveSchema(schema, '#/properties/description/properties/notfound'), undefined);
});
95 changes: 95 additions & 0 deletions packages/examples/src/anyOf-oneOf-allOf-resolve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
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 = {
$defs: {
Base: {
type: 'object',
properties: {
width: {
type: 'integer'
}
}
},
Child: {
type: 'object',
allOf: [
{ $ref: '#/$defs/Base' },
{
properties: {
geometry: {
type: 'string'
}
}
}
]
}
},
type: 'object',
properties: {
element: {
$ref: '#/$defs/Child'
}
}
};

export const uischema = {
type: 'VerticalLayout',
elements: [
{
type: 'Label',
text: 'AllOfRenderer'
},
{
type: 'Control',
scope: '#/properties/element'
},
{
type: 'Label',
text: 'Manual controls'
},
{
type: 'Control',
scope: '#/properties/element/properties/width'
},
{
type: 'Control',
scope: '#/properties/element/properties/geometry'
}
]
};

const data = {};

registerExamples([
{
name: 'anyOf-oneOf-allOf-resolve',
label: 'AnyOf OneOf AllOf Resolve',
data,
schema,
uischema
}
]);
2 changes: 2 additions & 0 deletions packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as allOf from './allOf';
import * as anyOf from './anyOf';
import * as oneOf from './oneOf';
import * as oneOfArray from './oneOfArray';
import * as anyOfOneOfAllOfResolve from './anyOf-oneOf-allOf-resolve';
import * as array from './arrays';
import * as nestedArray from './nestedArrays';
import * as arrayWithDetail from './arrays-with-detail';
Expand Down Expand Up @@ -81,6 +82,7 @@ export {
anyOf,
oneOf,
oneOfArray,
anyOfOneOfAllOfResolve,
stringArray,
array,
nestedArray,
Expand Down