From ecd23317c434620f911dd39a8127cd99bd4259dc Mon Sep 17 00:00:00 2001 From: Drew Hoover Date: Tue, 18 Jul 2023 16:54:44 -0400 Subject: [PATCH 1/8] Add a test for combinators and pass resolved subschema to createLabel --- packages/core/src/util/combinators.ts | 2 +- packages/core/test/util/combinators.test.ts | 49 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/core/test/util/combinators.test.ts diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index e502d229f..c990020aa 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -70,6 +70,6 @@ export const createCombinatorRenderInfos = ( control, rootSchema ), - label: createLabel(subSchema, subSchemaIndex, keyword), + label: createLabel(schema, subSchemaIndex, keyword), }; }); diff --git a/packages/core/test/util/combinators.test.ts b/packages/core/test/util/combinators.test.ts new file mode 100644 index 000000000..f9b765f8f --- /dev/null +++ b/packages/core/test/util/combinators.test.ts @@ -0,0 +1,49 @@ +import test from 'ava'; +import { createCombinatorRenderInfos } from '../../src/util/combinators'; +import { ControlElement } from '../../src'; + +const rootSchema = { + type: 'object', + properties: { + widget: { + anyOf: [ + { + $ref: '#/definitions/Dua', + }, + { + $ref: '#/definitions/Lipa', + }, + ], + }, + }, + definitions: { + Dua: { + title: 'Dua', + type: 'object', + properties: { name: { type: 'string' } }, + }, + Lipa: { + title: 'Lipa', + type: 'object', + properties: { name: { type: 'string' } }, + }, + }, +}; + +const control: ControlElement = { + type: 'Control', + scope: '#', +}; + +test('createCombinatorRenderInfos - uses titles for labels when subschemas are refs', (t) => { + const [duaRenderInfo, lipaRenderInfo] = createCombinatorRenderInfos( + rootSchema.properties.widget.anyOf, + rootSchema, + 'anyOf', + control, + 'widget', + [] + ); + t.deepEqual(duaRenderInfo.label, 'Dua'); + t.deepEqual(lipaRenderInfo.label, 'Lipa'); +}); From 70707c9b47655cf945b8939a648322d6ec63a46b Mon Sep 17 00:00:00 2001 From: Drew Hoover Date: Wed, 26 Jul 2023 23:28:16 -0400 Subject: [PATCH 2/8] add test case for property overrides defined on ref --- packages/core/src/util/combinators.ts | 18 +++++++---- packages/core/test/util/combinators.test.ts | 33 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index c990020aa..3d067ebd9 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -23,6 +23,7 @@ THE SOFTWARE. */ +import merge from 'lodash/merge'; import type { ControlElement, JsonSchema, UISchemaElement } from '../models'; import { findUISchema, JsonFormsUISchemaRegistryEntry } from '../reducers'; import { Resolve } from './util'; @@ -42,9 +43,8 @@ const createLabel = ( ): string => { if (subSchema.title) { return subSchema.title; - } else { - return keyword + '-' + subSchemaIndex; } + return keyword + '-' + subSchemaIndex; }; export const createCombinatorRenderInfos = ( @@ -56,9 +56,11 @@ export const createCombinatorRenderInfos = ( uischemas: JsonFormsUISchemaRegistryEntry[] ): CombinatorSubSchemaRenderInfo[] => combinatorSubSchemas.map((subSchema, subSchemaIndex) => { - const schema = subSchema.$ref - ? Resolve.schema(rootSchema, subSchema.$ref, rootSchema) - : subSchema; + const resolvedSubschema = + subSchema.$ref && Resolve.schema(rootSchema, subSchema.$ref, rootSchema); + + const schema = resolvedSubschema ?? subSchema; + return { schema, uischema: findUISchema( @@ -70,6 +72,10 @@ export const createCombinatorRenderInfos = ( control, rootSchema ), - label: createLabel(schema, subSchemaIndex, keyword), + label: createLabel( + merge({}, resolvedSubschema, subSchema), + subSchemaIndex, + keyword + ), }; }); diff --git a/packages/core/test/util/combinators.test.ts b/packages/core/test/util/combinators.test.ts index f9b765f8f..548760967 100644 --- a/packages/core/test/util/combinators.test.ts +++ b/packages/core/test/util/combinators.test.ts @@ -30,6 +30,26 @@ const rootSchema = { }, }; +const rootSchemaWithOverrides = { + ...rootSchema, + properties: { + ...rootSchema.properties, + widget: { + ...rootSchema.properties.widget, + anyOf: [ + { + ...rootSchema.properties.widget.anyOf[0], + title: 'DuaOverride', + }, + { + ...rootSchema.properties.widget.anyOf[1], + title: 'LipaOverride', + }, + ], + }, + }, +}; + const control: ControlElement = { type: 'Control', scope: '#', @@ -47,3 +67,16 @@ test('createCombinatorRenderInfos - uses titles for labels when subschemas are r t.deepEqual(duaRenderInfo.label, 'Dua'); t.deepEqual(lipaRenderInfo.label, 'Lipa'); }); + +test('createCombinatorRenderInfos - uses overrides for labels when subschemas are refs', (t) => { + const [duaRenderInfo, lipaRenderInfo] = createCombinatorRenderInfos( + rootSchemaWithOverrides.properties.widget.anyOf, + rootSchemaWithOverrides, + 'anyOf', + control, + 'widget', + [] + ); + t.deepEqual(duaRenderInfo.label, 'DuaOverride'); + t.deepEqual(lipaRenderInfo.label, 'LipaOverride'); +}); From 64a8ca4190b6f718838efa3a3e956a9526d7c91c Mon Sep 17 00:00:00 2001 From: Drew Hoover Date: Wed, 26 Jul 2023 23:33:21 -0400 Subject: [PATCH 3/8] fix name casing --- packages/core/src/util/combinators.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index 3d067ebd9..0298479dc 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -56,10 +56,10 @@ export const createCombinatorRenderInfos = ( uischemas: JsonFormsUISchemaRegistryEntry[] ): CombinatorSubSchemaRenderInfo[] => combinatorSubSchemas.map((subSchema, subSchemaIndex) => { - const resolvedSubschema = + const resolvedSubSchema = subSchema.$ref && Resolve.schema(rootSchema, subSchema.$ref, rootSchema); - const schema = resolvedSubschema ?? subSchema; + const schema = resolvedSubSchema ?? subSchema; return { schema, @@ -73,7 +73,7 @@ export const createCombinatorRenderInfos = ( rootSchema ), label: createLabel( - merge({}, resolvedSubschema, subSchema), + merge({}, resolvedSubSchema, subSchema), subSchemaIndex, keyword ), From 93eb6286949dbd2e7f6d7a27b13996346c9857a1 Mon Sep 17 00:00:00 2001 From: Stefan Dirix Date: Mon, 7 Aug 2023 16:52:44 +0200 Subject: [PATCH 4/8] Update packages/core/src/util/combinators.ts --- packages/core/src/util/combinators.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index 0298479dc..4e8b9fa63 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -72,10 +72,6 @@ export const createCombinatorRenderInfos = ( control, rootSchema ), - label: createLabel( - merge({}, resolvedSubSchema, subSchema), - subSchemaIndex, - keyword - ), + label: subSchema.title ?? resolvedSubSchema.title ?? `${keyword}-${subSchemaIndex}` }; }); From effeb8d94d9e2566ab3f5720552cfffcbea8ebfa Mon Sep 17 00:00:00 2001 From: Stefan Dirix Date: Mon, 7 Aug 2023 16:53:07 +0200 Subject: [PATCH 5/8] Update combinators.ts --- packages/core/src/util/combinators.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index 4e8b9fa63..0ec64070d 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -36,17 +36,6 @@ export interface CombinatorSubSchemaRenderInfo { export type CombinatorKeyword = 'anyOf' | 'oneOf' | 'allOf'; -const createLabel = ( - subSchema: JsonSchema, - subSchemaIndex: number, - keyword: CombinatorKeyword -): string => { - if (subSchema.title) { - return subSchema.title; - } - return keyword + '-' + subSchemaIndex; -}; - export const createCombinatorRenderInfos = ( combinatorSubSchemas: JsonSchema[], rootSchema: JsonSchema, From ba8cb0d92e41bcda6efe46eba37d567c77615756 Mon Sep 17 00:00:00 2001 From: Stefan Dirix Date: Mon, 7 Aug 2023 16:54:19 +0200 Subject: [PATCH 6/8] Update combinators.ts --- packages/core/src/util/combinators.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index 0ec64070d..85f097d03 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -23,7 +23,6 @@ THE SOFTWARE. */ -import merge from 'lodash/merge'; import type { ControlElement, JsonSchema, UISchemaElement } from '../models'; import { findUISchema, JsonFormsUISchemaRegistryEntry } from '../reducers'; import { Resolve } from './util'; From 155c88b9483362c5e6ed8cf988ef44e435e99e0a Mon Sep 17 00:00:00 2001 From: Stefan Dirix Date: Wed, 9 Aug 2023 00:36:26 +0200 Subject: [PATCH 7/8] fix lint --- packages/core/src/util/combinators.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index 85f097d03..9ebb3b6cc 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -60,6 +60,9 @@ export const createCombinatorRenderInfos = ( control, rootSchema ), - label: subSchema.title ?? resolvedSubSchema.title ?? `${keyword}-${subSchemaIndex}` + label: + subSchema.title ?? + resolvedSubSchema.title ?? + `${keyword}-${subSchemaIndex}`, }; }); From 4226acf57150f31573120e50b2013b42e08aad11 Mon Sep 17 00:00:00 2001 From: Drew Hoover Date: Wed, 9 Aug 2023 09:54:25 -0400 Subject: [PATCH 8/8] add test for base case & fix typeerror --- packages/core/src/util/combinators.ts | 2 +- packages/core/test/util/combinators.test.ts | 31 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/core/src/util/combinators.ts b/packages/core/src/util/combinators.ts index 9ebb3b6cc..60cb0f7f9 100644 --- a/packages/core/src/util/combinators.ts +++ b/packages/core/src/util/combinators.ts @@ -62,7 +62,7 @@ export const createCombinatorRenderInfos = ( ), label: subSchema.title ?? - resolvedSubSchema.title ?? + resolvedSubSchema?.title ?? `${keyword}-${subSchemaIndex}`, }; }); diff --git a/packages/core/test/util/combinators.test.ts b/packages/core/test/util/combinators.test.ts index 548760967..8b9115683 100644 --- a/packages/core/test/util/combinators.test.ts +++ b/packages/core/test/util/combinators.test.ts @@ -80,3 +80,34 @@ test('createCombinatorRenderInfos - uses overrides for labels when subschemas ar t.deepEqual(duaRenderInfo.label, 'DuaOverride'); t.deepEqual(lipaRenderInfo.label, 'LipaOverride'); }); + +const schemaWithoutRefs = { + type: 'object', + properties: { + widget: { + anyOf: [ + { + type: 'object', + properties: { name: { type: 'string' } }, + }, + { + type: 'object', + properties: { name: { type: 'string' } }, + }, + ], + }, + }, +}; + +test('createCombinatorRenderInfos - uses keyword + index when no labels provided', (t) => { + const [duaRenderInfo, lipaRenderInfo] = createCombinatorRenderInfos( + schemaWithoutRefs.properties.widget.anyOf, + schemaWithoutRefs, + 'anyOf', + control, + 'widget', + [] + ); + t.deepEqual(duaRenderInfo.label, 'anyOf-0'); + t.deepEqual(lipaRenderInfo.label, 'anyOf-1'); +});