Skip to content

Commit

Permalink
fix(core): Recognize enums without type-attribute
Browse files Browse the repository at this point in the history
Previously, enums without type-attribute weren't recognized by the
derive types function, leading to omitted enums in generated uischemas.

Closes #2177
  • Loading branch information
LukasBoll authored and lucas-koehler committed Oct 25, 2023
1 parent deb2c7c commit f905c82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/core/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ export const deriveTypes = (jsonSchema: JsonSchema): string[] => {
if (!isEmpty(jsonSchema.items)) {
return ['array'];
}

if (!isEmpty(jsonSchema.enum)) {
const types: Set<string> = new Set();
jsonSchema.enum.forEach((enumElement) => {
if (typeof enumElement === 'string') {
types.add('string');
} else {
deriveTypes(enumElement).forEach((type) => types.add(type));
}
});
return Array.from(types);
}
if (!isEmpty(jsonSchema.allOf)) {
const allOfType = find(
jsonSchema.allOf,
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/util/derivetype.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ test('derive type w/o type - items array', (t) => {
t.is(deriveTypes(schema)[0], 'array');
});

test('derive type w/o type - enum', (t) => {
const schema: JsonSchema = {
enum: ['foo', 'bar'],
};
t.is(deriveTypes(schema).length, 1);
t.is(deriveTypes(schema)[0], 'string');
});

test('derive type w/o type - enum with two types', (t) => {
const schema: JsonSchema = {
enum: ['foo', 'bar', { properties: { foo: { type: 'string' } } }],
};
t.is(deriveTypes(schema).length, 2);
t.is(deriveTypes(schema)[0], 'string');
t.is(deriveTypes(schema)[1], 'object');
});

test('derive type with type - union', (t) => {
const schema: JsonSchema = {
type: ['string', 'number'],
Expand Down

0 comments on commit f905c82

Please sign in to comment.