From f664a76d554f1c33cfcbcbcd542e9d1dd1374923 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Tue, 17 Oct 2023 13:39:21 +0500 Subject: [PATCH] fix: handle enum Signed-off-by: Muhammad Aaqil --- packages/cli/generators/discover/index.js | 20 +++++++++++++++- .../discover/import-discovered-model.test.js | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/cli/generators/discover/index.js b/packages/cli/generators/discover/index.js index c6be9a9d4ed6..65acc43be108 100644 --- a/packages/cli/generators/discover/index.js +++ b/packages/cli/generators/discover/index.js @@ -414,7 +414,25 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator { templateData.settings, ); } - + Object.keys(templateData.properties).forEach(key => { + const property = templateData.properties[key]; + // if the type is enum + if (property.type.startsWith(`'enum`)) { + property.type = property.type.slice(1, -1); + const enumRemoved = property.type.split(`enum`)[1]; + const enumValues = enumRemoved.slice(1, -1).replaceAll(`'`, ''); + const enumValuesArray = enumValues.split(','); + let enumItems = ''; + enumValuesArray.forEach(item => { + enumItems += `'${item}',`; + }); + templateData.properties[key]['type'] = 'String'; + templateData.properties[key]['tsType'] = 'string'; + templateData.properties[key][ + 'jsonSchema' + ] = `{enum: [${enumItems.toString()}]}`; + } + }); this.copyTemplatedFiles( modelDiscoverer.MODEL_TEMPLATE_PATH, fullPath, diff --git a/packages/cli/test/unit/discover/import-discovered-model.test.js b/packages/cli/test/unit/discover/import-discovered-model.test.js index 8a905cf6cb14..69c63a938767 100644 --- a/packages/cli/test/unit/discover/import-discovered-model.test.js +++ b/packages/cli/test/unit/discover/import-discovered-model.test.js @@ -157,6 +157,29 @@ describe('importDiscoveredModel', () => { }); }); + it('imports enum properties', () => { + const discoveredModel = { + name: 'TestModel', + properties: { + patient: { + type: 'String', + jsonSchema: {enum: ['INPATIENT', 'OUTPATIENT']}, + required: false, + length: null, + precision: null, + scale: null, + }, + }, + }; + + const modelData = importDiscoveredModel(discoveredModel); + expect(modelData.properties).to.have.property('patient').deepEqual({ + jsonSchema: "{enum: ['INPATIENT', 'OUTPATIENT']}", + type: `'string'`, + tsType: 'string', + }); + }); + it('converts connector metadata to TypeScript object', () => { const discoveredModel = { name: 'TestModel',