Skip to content

Commit

Permalink
generate schemas for enumerations
Browse files Browse the repository at this point in the history
per #125
  • Loading branch information
kshychko committed May 4, 2020
1 parent c8a20e1 commit 4020fd5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
58 changes: 29 additions & 29 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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()) {
/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -408,7 +409,6 @@ class Component {
newAssArr = newSort.concat(otherSort);
return newAssArr;
}

/**
* @function getJSONSchema
* @description Returns component object
Expand Down
32 changes: 26 additions & 6 deletions src/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Properties {
this.objClass = objClass;
this.assocSideClassLink = assocSideClassLink;
this.arrAttRequired = [];
this.enumerations = [];
utils.resetErrorBlock();
}

Expand All @@ -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
Expand Down Expand Up @@ -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 != "") {
Expand Down

0 comments on commit 4020fd5

Please sign in to comment.