Skip to content

Commit f905c82

Browse files
LukasBolllucas-koehler
authored andcommitted
fix(core): Recognize enums without type-attribute
Previously, enums without type-attribute weren't recognized by the derive types function, leading to omitted enums in generated uischemas. Closes #2177
1 parent deb2c7c commit f905c82

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/core/src/util/util.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,17 @@ export const deriveTypes = (jsonSchema: JsonSchema): string[] => {
7777
if (!isEmpty(jsonSchema.items)) {
7878
return ['array'];
7979
}
80-
80+
if (!isEmpty(jsonSchema.enum)) {
81+
const types: Set<string> = new Set();
82+
jsonSchema.enum.forEach((enumElement) => {
83+
if (typeof enumElement === 'string') {
84+
types.add('string');
85+
} else {
86+
deriveTypes(enumElement).forEach((type) => types.add(type));
87+
}
88+
});
89+
return Array.from(types);
90+
}
8191
if (!isEmpty(jsonSchema.allOf)) {
8292
const allOfType = find(
8393
jsonSchema.allOf,

packages/core/test/util/derivetype.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ test('derive type w/o type - items array', (t) => {
6565
t.is(deriveTypes(schema)[0], 'array');
6666
});
6767

68+
test('derive type w/o type - enum', (t) => {
69+
const schema: JsonSchema = {
70+
enum: ['foo', 'bar'],
71+
};
72+
t.is(deriveTypes(schema).length, 1);
73+
t.is(deriveTypes(schema)[0], 'string');
74+
});
75+
76+
test('derive type w/o type - enum with two types', (t) => {
77+
const schema: JsonSchema = {
78+
enum: ['foo', 'bar', { properties: { foo: { type: 'string' } } }],
79+
};
80+
t.is(deriveTypes(schema).length, 2);
81+
t.is(deriveTypes(schema)[0], 'string');
82+
t.is(deriveTypes(schema)[1], 'object');
83+
});
84+
6885
test('derive type with type - union', (t) => {
6986
const schema: JsonSchema = {
7087
type: ['string', 'number'],

0 commit comments

Comments
 (0)