Skip to content

Commit

Permalink
Support overridable combinator labels
Browse files Browse the repository at this point in the history
'title' can be defined in both the resolving subschema and the resolved to subschema.
This fix enhances JSON Forms to take both of these cases into account when resolving
subschemas in combinators. Intermediate titles will still be ignored.

Fixes #2164
  • Loading branch information
DrewHoo authored Aug 8, 2023
1 parent d8d261e commit 22d14d3
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 16 deletions.
25 changes: 9 additions & 16 deletions packages/core/src/util/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,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;
} else {
return keyword + '-' + subSchemaIndex;
}
};

export const createCombinatorRenderInfos = (
combinatorSubSchemas: JsonSchema[],
rootSchema: JsonSchema,
Expand All @@ -56,9 +44,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(
Expand All @@ -70,6 +60,9 @@ export const createCombinatorRenderInfos = (
control,
rootSchema
),
label: createLabel(subSchema, subSchemaIndex, keyword),
label:
subSchema.title ??
resolvedSubSchema.title ??
`${keyword}-${subSchemaIndex}`,
};
});
82 changes: 82 additions & 0 deletions packages/core/test/util/combinators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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 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: '#',
};

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');
});

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');
});

0 comments on commit 22d14d3

Please sign in to comment.