diff --git a/src/component.js b/src/component.js index 0865c1c..1cae2ea 100644 --- a/src/component.js +++ b/src/component.js @@ -103,13 +103,6 @@ class Component { return this.mainComponentObj; } - - /** - * @function addAttrTypeRefClassSchema - * @description Add schema of such classes of which attribute type is refference of UMLClass - * @param {Array} refClasses - * @memberof Component - */ addAttrTypeRefClassSchema(refClasses) { refClasses.forEach(objClass => { let mainClassesObj = {}; @@ -143,18 +136,6 @@ class Component { }); } - - /** - * @function addClassSchema - * @description Add schema of such classes of which attribute type is refference of UMLClass - * @param {Array} classEleOrViews - * @param {Object} assoClassLink - * @param {Object} mAssoClassLinkView - * @param {Array} arrIdClasses - * @param {Array} duplicateDeletedReference - * @param {Array} duplicatePropertyError - * @memberof Component - */ addClassSchema(classEleOrViews, assoClassLink, mAssoClassLinkView, arrIdClasses, duplicateDeletedReference, duplicatePropertyError) { classEleOrViews.forEach(classEleOrView => { let mainClassesObj = {}; @@ -220,7 +201,35 @@ class Component { let compositionRef = []; let arrAttributes = properties.getAttributes(); - + let arrEnumerations = properties.getEnumerations(); + arrEnumerations.forEach(enumeration => { + if(this.mainSchemaObj[enumeration.name] == null){ + let enumerationObj = {} + enumerationObj.enum = utils.getEnumerationLiteral(enumeration); + enumerationObj.description = enumerationObj.documentation ? utils.buildDescription(enumerationObj.documentation) : constant.STR_MISSING_DESCRIPTION; + enumerationObj.type = 'string'; + if(enumerationObj.enum.length == 0) { + /** + * Check if the enumeration has a tag named 'ref'. If it presents use it as '$ref' value instead of including the enum to the specification + */ + let refTag; + if(enumeration.tags != null && enumeration.tags.length > 0 ){ + forEach(enumeration.tags, function (tag) { + if(tag.name === 'ref') { + refTag = tag.value; + } + }); + } + if( refTag != null) { + enumerationObj = {} + enumerationObj.$ref = refTag; + } else { + app.dialogs.showAlertDialog('Enumeration ' + enumeration.name + ' has no values. You can add the enumeration to the diagram to include it or specify "ref" tag to refer to the external definition.'); + } + } + this.mainSchemaObj[enumeration.name] = enumerationObj; + } + }); if (openAPI.isModelPackage()) { /** @@ -372,14 +381,6 @@ class Component { }); } - /** - * @function sortAssociationByModelExplorerSequence - * @description Add schema of such classes of which attribute type is refference of UMLClass - * @param {Array} objClass - * @param {Object} classAssociations - * @returns {Array} newAssArr - * @memberof Component - */ sortAssociationByModelExplorerSequence(objClass, classAssociations) { let newAssArr = []; let newClassAssociation = classAssociations; @@ -408,7 +409,6 @@ class Component { newAssArr = newSort.concat(otherSort); return newAssArr; } - /** * @function getJSONSchema * @description Returns component object diff --git a/src/properties.js b/src/properties.js index ca4ae83..ce6ee8f 100644 --- a/src/properties.js +++ b/src/properties.js @@ -15,6 +15,7 @@ class Properties { this.objClass = objClass; this.assocSideClassLink = assocSideClassLink; this.arrAttRequired = []; + this.enumerations = []; utils.resetErrorBlock(); } @@ -28,6 +29,16 @@ class Properties { return this.arrAttRequired; } + /** + * @function getEnumerations + * @description Returns the array of enumerations used as data types by properties + * @returns {Array} + * @memberof Properties + */ + getEnumerations() { + return this.enumerations; + } + /** * @function addProperties * @description Adds properties to mainPropertiesObject @@ -141,13 +152,22 @@ class Properties { propertiesObj.minItems = 1; } } else { - propertiesObj.description = (attribute.documentation ? utils.buildDescription(attribute.documentation) : constant.STR_MISSING_DESCRIPTION); - - utils.addAttributeType(propertiesObj, attribute); - + /** + * If data type is enumeration, build allOf object with a reference to a corresponding schema + */ if (attribute.type instanceof type.UMLEnumeration) { - /* Add Enumeration */ - propertiesObj.enum = utils.getEnumerationLiteral(attribute.type); + let allOfArr = []; + let descriptionObj = {}; + let refObj = {}; + descriptionObj['description'] = attribute.documentation ? utils.buildDescription(attribute.documentation) : constant.STR_MISSING_DESCRIPTION; + refObj['$ref'] = '#/components/schemas/' + attribute.type.name; + _this.enumerations.push(attribute.type); + allOfArr.push(descriptionObj); + allOfArr.push(refObj); + propertiesObj['allOf'] = allOfArr; + } else{ + propertiesObj.description = (attribute.documentation ? utils.buildDescription(attribute.documentation) : constant.STR_MISSING_DESCRIPTION); + utils.addAttributeType(propertiesObj, attribute); } } if (attribute.defaultValue != "") {