From 1aa8bc6e71467d65a05c3d283ce60cb7daaa4e8b Mon Sep 17 00:00:00 2001 From: chengxiaotian Date: Thu, 13 Jun 2019 17:39:53 +0800 Subject: [PATCH] divide parameter and response into two object --- lib/apidocToSwagger.js | 50 ++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/apidocToSwagger.js b/lib/apidocToSwagger.js index 89c6380..32843cb 100644 --- a/lib/apidocToSwagger.js +++ b/lib/apidocToSwagger.js @@ -8,7 +8,7 @@ var swagger = { definitions: {} }; -function toSwagger(apidocJson, projectJson) { +function toSwagger(apidocJson, projectJson) { swagger.info = addInfo(projectJson); swagger.paths = extractPaths(apidocJson); return swagger; @@ -110,10 +110,8 @@ function createPostPushPutOutput(verbs, definitions, pathKeys) { "200": { "description": "successful operation", "schema": { - "type": verbDefinitionResult.topLevelSuccessRefType, - "items": { + "type": 'object', "$ref": "#/definitions/" + verbDefinitionResult.topLevelSuccessRef - } } } }; @@ -128,16 +126,15 @@ function createVerbDefinitions(verbs, definitions) { topLevelSuccessRef : null, topLevelSuccessRefType : null }; - var defaultObjectName = verbs.name; - + var defaultObjectName = verbs.name + 'Default'; var fieldArrayResult = {}; if (verbs && verbs.parameter && verbs.parameter.fields) { - fieldArrayResult = createFieldArrayDefinitions(verbs.parameter.fields.Parameter, definitions, verbs.name, defaultObjectName); + fieldArrayResult = createFieldArrayDefinitions(verbs.parameter.fields.Parameter, definitions, verbs.name + 'Parameter', defaultObjectName); result.topLevelParametersRef = fieldArrayResult.topLevelRef; }; if (verbs && verbs.success && verbs.success.fields) { - fieldArrayResult = createFieldArrayDefinitions(verbs.success.fields["Success 200"], definitions, verbs.name, defaultObjectName); + fieldArrayResult = createFieldArrayDefinitions(verbs.success.fields["Success 200"], definitions, verbs.name + 'Res', defaultObjectName); result.topLevelSuccessRef = fieldArrayResult.topLevelRef; result.topLevelSuccessRefType = fieldArrayResult.topLevelRefType; }; @@ -154,43 +151,49 @@ function createFieldArrayDefinitions(fieldArray, definitions, topLevelRef, defau if (!fieldArray) { return result; } - for (var i = 0; i < fieldArray.length; i++) { var parameter = fieldArray[i]; - var nestedName = createNestedName(parameter.field); + var nestedName = createNestedName(parameter.field, topLevelRef); + var objectName = nestedName.objectName; if (!objectName) { objectName = defaultObjectName; } var type = parameter.type; + if (i == 0) { result.topLevelRefType = type; - if(parameter.type == "Object") { + if(parameter.type == "Object" || parameter.type == "object") { + result.topLevelRefType = "object"; objectName = nestedName.propertyName; nestedName.propertyName = null; - } else if (parameter.type == "Array") { + } else if (parameter.type == "Array" || parameter.type.indexOf("[]") !=-1) { objectName = nestedName.propertyName; nestedName.propertyName = null; result.topLevelRefType = "array"; } result.topLevelRef = objectName; }; + definitions[objectName] = definitions[objectName] || { properties : {}, required : [] }; - if (nestedName.propertyName) { var prop = { type: (parameter.type || "").toLowerCase(), description: removeTags(parameter.description) }; - if(parameter.type == "Object") { - prop.$ref = "#/definitions/" + parameter.field; + var proName = parameter.field.split(".").pop(); + if(parameter.type.toLowerCase() == "object") { + prop.$ref = "#/definitions/" + objectName + '.' + proName; } var typeIndex = type.indexOf("[]"); if(typeIndex !== -1 && typeIndex === (type.length - 2)) { prop.type = "array"; + + prop.items = { - type: type.slice(0, type.length-2) + type: type.slice(0, type.length-2), + $ref: "#/definitions/" + objectName + '.' + proName }; } @@ -208,16 +211,17 @@ function createFieldArrayDefinitions(fieldArray, definitions, topLevelRef, defau return result; } -function createNestedName(field) { +function createNestedName(field, topLevelRef) { var propertyName = field; var objectName; + var propertyNames = field.split("."); if(propertyNames && propertyNames.length > 1) { propertyName = propertyNames[propertyNames.length-1]; propertyNames.pop(); objectName = propertyNames.join("."); - } - + } + objectName = (objectName == undefined) ? topLevelRef : topLevelRef + '.' + objectName return { propertyName: propertyName, objectName: objectName @@ -247,14 +251,13 @@ function createGetDeleteOutput(verbs,definitions) { parameters: createPathParameters(verbs) } if (verbDefinitionResult.topLevelSuccessRef) { + pathItemObject[verbs.type].responses = { "200": { "description": "successful operation", "schema": { - "type": verbDefinitionResult.topLevelSuccessRefType, - "items": { - "$ref": "#/definitions/" + verbDefinitionResult.topLevelSuccessRef - } + "type": 'object', + "$ref": "#/definitions/" + verbDefinitionResult.topLevelSuccessRef } } }; @@ -269,7 +272,6 @@ function createGetDeleteOutput(verbs,definitions) { */ function createPathParameters(verbs, pathKeys) { pathKeys = pathKeys || []; - var pathItemObject = []; if (verbs.parameter && verbs.parameter.fields.Parameter) {