Skip to content

Commit c060472

Browse files
LukasBolllucas-koehler
authored andcommitted
fix: resolve refs in EnumArrayRenderer
resolve refs ind the MultiEnumControlProps mapper and tester fixes #2192
1 parent f905c82 commit c060472

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

packages/core/src/util/renderer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import {
7878
arrayDefaultTranslations,
7979
ArrayTranslations,
8080
} from '../i18n/arrayTranslations';
81+
import { resolveSchema } from './resolvers';
8182

8283
const isRequired = (
8384
schema: JsonSchema,
@@ -617,7 +618,11 @@ export const mapStateToMultiEnumControlProps = (
617618
ownProps: OwnPropsOfControl & OwnPropsOfEnum
618619
): StatePropsOfControl & OwnPropsOfEnum => {
619620
const props: StatePropsOfControl = mapStateToControlProps(state, ownProps);
620-
const items = props.schema.items as JsonSchema;
621+
let items = props.schema.items as JsonSchema;
622+
items =
623+
items && items.$ref
624+
? resolveSchema(props.rootSchema, items.$ref, props.rootSchema)
625+
: items;
621626
const options: EnumOption[] =
622627
ownProps.options ||
623628
(items?.oneOf &&

packages/core/test/util/renderer.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,50 @@ test('mapStateToMultiEnumControlProps - enum items', (t) => {
983983
]);
984984
});
985985

986+
test('mapStateToMultiEnumControlProps - enum with ref', (t) => {
987+
const uischema: ControlElement = {
988+
type: 'Control',
989+
scope: '#/properties/colors',
990+
};
991+
const state = {
992+
jsonforms: {
993+
core: {
994+
schema: {
995+
definitions: {
996+
colors: {
997+
type: 'string',
998+
enum: ['red', 'green', 'pink'],
999+
},
1000+
},
1001+
type: 'object',
1002+
properties: {
1003+
colors: {
1004+
type: 'array',
1005+
items: {
1006+
$ref: '#/definitions/colors',
1007+
},
1008+
uniqueItems: true,
1009+
},
1010+
},
1011+
},
1012+
data: {},
1013+
uischema,
1014+
errors: [] as ErrorObject[],
1015+
},
1016+
},
1017+
};
1018+
const ownProps = {
1019+
uischema,
1020+
path: 'colors',
1021+
};
1022+
const props = mapStateToMultiEnumControlProps(state, ownProps);
1023+
t.deepEqual(props.options, [
1024+
{ label: 'red', value: 'red' },
1025+
{ label: 'green', value: 'green' },
1026+
{ label: 'pink', value: 'pink' },
1027+
]);
1028+
});
1029+
9861030
test('mapDispatchToMultiEnumProps - enum schema - addItem', (t) => {
9871031
const uischema: ControlElement = {
9881032
type: 'Control',

packages/material-renderers/src/complex/MaterialEnumArrayRenderer.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Paths,
99
RankedTester,
1010
rankWith,
11+
resolveSchema,
1112
schemaMatches,
1213
schemaSubPathMatches,
1314
uiTypeIs,
@@ -99,8 +100,11 @@ export const materialEnumArrayRendererTester: RankedTester = rankWith(
99100
!Array.isArray(schema.items) &&
100101
schema.uniqueItems === true
101102
),
102-
schemaSubPathMatches('items', (schema) => {
103-
return hasOneOfItems(schema) || hasEnumItems(schema);
103+
schemaSubPathMatches('items', (schema, rootSchema) => {
104+
const resolvedSchema = schema.$ref
105+
? resolveSchema(rootSchema, schema.$ref, rootSchema)
106+
: schema;
107+
return hasOneOfItems(resolvedSchema) || hasEnumItems(resolvedSchema);
104108
})
105109
)
106110
)

0 commit comments

Comments
 (0)