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

divide parameter and response into two object #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
50 changes: 26 additions & 24 deletions lib/apidocToSwagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,10 +110,8 @@ function createPostPushPutOutput(verbs, definitions, pathKeys) {
"200": {
"description": "successful operation",
"schema": {
"type": verbDefinitionResult.topLevelSuccessRefType,
"items": {
"type": 'object',
"$ref": "#/definitions/" + verbDefinitionResult.topLevelSuccessRef
}
}
}
};
Expand All @@ -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;
};
Expand All @@ -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
};
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
};
Expand All @@ -269,7 +272,6 @@ function createGetDeleteOutput(verbs,definitions) {
*/
function createPathParameters(verbs, pathKeys) {
pathKeys = pathKeys || [];

var pathItemObject = [];
if (verbs.parameter && verbs.parameter.fields.Parameter) {

Expand Down