diff --git a/forward_engineering/helpers/typeHelper.js b/forward_engineering/helpers/typeHelper.js index 20aecb6..aa3ed7f 100644 --- a/forward_engineering/helpers/typeHelper.js +++ b/forward_engineering/helpers/typeHelper.js @@ -36,7 +36,6 @@ function getTypeProps(data, key, isParentActivated) { maxItems: data.maxItems, uniqueItems: data.uniqueItems || undefined, nullable: data.nullable, - discriminator: data.discriminator, readOnly: data.readOnly, example: parseExample(data.sample) || getArrayItemsExample(items), xml: getXml(data.xml) @@ -46,6 +45,7 @@ function getTypeProps(data, key, isParentActivated) { return Object.assign({}, arrayProps, arrayChoices, extensions); } case 'object': { + const discriminator = getDiscriminator(data.discriminator); const objectProps = { type, title: data.title || undefined, @@ -56,7 +56,7 @@ function getTypeProps(data, key, isParentActivated) { maxProperties: data.maxProperties, additionalProperties: getAdditionalProperties(data), nullable: data.nullable, - discriminator: data.discriminator, + ...(discriminator ? { discriminator } : {}), readOnly: data.readOnly, example: parseExample(data.sample), xml: getXml(data.xml) @@ -239,6 +239,22 @@ function getArrayItemsExample(items) { } } +function getDiscriminator(discriminator) { + if (!discriminator || !discriminator.propertyName) { + return null; + } + const mapping = (!discriminator.mapping || discriminator.mapping.length === 0) ? null : discriminator.mapping.reduce((acc, mappingItem) => { + acc[mappingItem.discriminatorValue] = mappingItem.discriminatorSchema; + return acc; + }, {}); + + return { + propertyName: discriminator.propertyName, + ...(mapping && { mapping }) + + }; +} + module.exports = { getType, getRef, diff --git a/properties_pane/field_level/fieldLevelConfig.json b/properties_pane/field_level/fieldLevelConfig.json index ad6089b..ceb1a6c 100644 --- a/properties_pane/field_level/fieldLevelConfig.json +++ b/properties_pane/field_level/fieldLevelConfig.json @@ -328,13 +328,6 @@ making sure that you maintain a proper JSON format. "propertyType": "checkbox", "enableForReference": true }, - { - "propertyName": "discriminator", - "propertyKeyword": "discriminator", - "propertyType": "text", - "sampleGen": "&containerName|&entityName|&random|", - "enableForReference": true - }, { "propertyName": "readOnly", "propertyKeyword": "readOnly", @@ -607,13 +600,6 @@ making sure that you maintain a proper JSON format. "propertyType": "checkbox", "enableForReference": true }, - { - "propertyName": "discriminator", - "propertyKeyword": "discriminator", - "propertyType": "text", - "sampleGen": "&containerName|&entityName|&random|", - "enableForReference": true - }, { "propertyName": "readOnly", "propertyKeyword": "readOnly", @@ -887,13 +873,6 @@ making sure that you maintain a proper JSON format. "propertyType": "checkbox", "enableForReference": true }, - { - "propertyName": "discriminator", - "propertyKeyword": "discriminator", - "propertyType": "text", - "sampleGen": "&containerName|&entityName|&random|", - "enableForReference": true - }, { "propertyName": "readOnly", "propertyKeyword": "readOnly", @@ -1273,17 +1252,6 @@ making sure that you maintain a proper JSON format. "value": "schema" } }, - { - "propertyName": "discriminator", - "propertyKeyword": "discriminator", - "propertyType": "text", - "sampleGen": "&containerName|&entityName|&random|", - "enableForReference": true, - "dependency": { - "key": "subtype", - "value": "schema" - } - }, { "propertyName": "readOnly", "propertyKeyword": "readOnly", @@ -1592,10 +1560,34 @@ making sure that you maintain a proper JSON format. } }, { - "propertyName": "discriminator", + "propertyName": "Discriminator", "propertyKeyword": "discriminator", - "propertyType": "text", - "sampleGen": "&containerName|&entityName|&random|", + "propertyType": "block", + "structure": [ + { + "propertyName": "Property name", + "propertyKeyword": "propertyName", + "propertyTooltip": "", + "propertyType": "text" + }, + { + "propertyName": "Mapping", + "propertyType": "group", + "propertyKeyword": "mapping", + "structure": [ + { + "propertyName": "Discriminator value", + "propertyKeyword": "discriminatorValue", + "propertyType": "text" + }, + { + "propertyName": "Schema name or reference", + "propertyKeyword": "discriminatorSchema", + "propertyType": "text" + } + ] + } + ], "dependency": { "key": "subtype", "value": "schema" diff --git a/reverse_engineering/helpers/dataHelper.js b/reverse_engineering/helpers/dataHelper.js index 3db3ad5..d52699b 100644 --- a/reverse_engineering/helpers/dataHelper.js +++ b/reverse_engineering/helpers/dataHelper.js @@ -545,6 +545,8 @@ const handleSchemaProperty = (property, data) => { return handleSchemaXml(data); case '$ref': return resolveReference(data); + case 'discriminator': + return handleDiscriminator(data); default: if (commonHelper.CHOICES.find(choice => data[choice])) { return handleSchemaProps(data); @@ -920,6 +922,18 @@ const validateOpenAPISchema = (schema) => { return isCorrectVersion; }; +const handleDiscriminator = (discriminator) => { + if (discriminator) { + const { propertyName, mapping } = discriminator; + const preparedMapping = mapping ? Object.keys(mapping).reduce((acc, key) => { + acc.push({ discriminatorValue: key, discriminatorSchema: mapping[key] }); + return acc; + }, []) : []; + return { propertyName, mapping: preparedMapping }; + } + return {}; +}; + module.exports = { getModelData, getComponents,