Skip to content

Commit f664a76

Browse files
aaqilnizsamarpanB
authored andcommitted
fix: handle enum
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
1 parent 368b5b6 commit f664a76

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

packages/cli/generators/discover/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,25 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
414414
templateData.settings,
415415
);
416416
}
417-
417+
Object.keys(templateData.properties).forEach(key => {
418+
const property = templateData.properties[key];
419+
// if the type is enum
420+
if (property.type.startsWith(`'enum`)) {
421+
property.type = property.type.slice(1, -1);
422+
const enumRemoved = property.type.split(`enum`)[1];
423+
const enumValues = enumRemoved.slice(1, -1).replaceAll(`'`, '');
424+
const enumValuesArray = enumValues.split(',');
425+
let enumItems = '';
426+
enumValuesArray.forEach(item => {
427+
enumItems += `'${item}',`;
428+
});
429+
templateData.properties[key]['type'] = 'String';
430+
templateData.properties[key]['tsType'] = 'string';
431+
templateData.properties[key][
432+
'jsonSchema'
433+
] = `{enum: [${enumItems.toString()}]}`;
434+
}
435+
});
418436
this.copyTemplatedFiles(
419437
modelDiscoverer.MODEL_TEMPLATE_PATH,
420438
fullPath,

packages/cli/test/unit/discover/import-discovered-model.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ describe('importDiscoveredModel', () => {
157157
});
158158
});
159159

160+
it('imports enum properties', () => {
161+
const discoveredModel = {
162+
name: 'TestModel',
163+
properties: {
164+
patient: {
165+
type: 'String',
166+
jsonSchema: {enum: ['INPATIENT', 'OUTPATIENT']},
167+
required: false,
168+
length: null,
169+
precision: null,
170+
scale: null,
171+
},
172+
},
173+
};
174+
175+
const modelData = importDiscoveredModel(discoveredModel);
176+
expect(modelData.properties).to.have.property('patient').deepEqual({
177+
jsonSchema: "{enum: ['INPATIENT', 'OUTPATIENT']}",
178+
type: `'string'`,
179+
tsType: 'string',
180+
});
181+
});
182+
160183
it('converts connector metadata to TypeScript object', () => {
161184
const discoveredModel = {
162185
name: 'TestModel',

0 commit comments

Comments
 (0)