Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle enum #10065

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/test/unit/discover/import-discovered-model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down