Skip to content

Commit

Permalink
transforms apib variable property name to swagger additional properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nazieb committed Oct 24, 2017
1 parent 66bcef4 commit bab2ba4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "optimusjs",
"version": "0.4.5",
"version": "0.4.6",
"description": "Transform API Blueprint into any other formats",
"main": "dist/index.js",
"bin": {
Expand Down
51 changes: 41 additions & 10 deletions src/formatters/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,13 @@ function getDefinitions(dataStructures) {

const properties = {};
for (let content of structure.content) {
if (content.element != "member") {
if (content.element !== "member") {
continue;
}

const property = {
"description": content.meta.description || "",
};
let property = {};

const memberName = content.content.key.content;

const memberType = content.content.value.element;
if (isPrimitiveType(memberType)) {
property["type"] = memberType;
Expand All @@ -378,23 +375,26 @@ function getDefinitions(dataStructures) {
}
} else if (isInheritedType(memberType)) {
property["$ref"] = convertDefinitionPath(memberType);
} else if (memberType == "enum") {
} else if (memberType === "enum") {
const enums = convertEnum(content.content.value.content);

property["type"] = enums["type"];
property["enum"] = enums["enum"];
} else if (memberType == "array") {
} else if (memberType === "array") {
property["type"] = memberType;
const itemsType = content.content.value.content[0].element;
property["items"] = isPrimitiveType(itemsType) || itemsType == "object" ? {
property["items"] = isPrimitiveType(itemsType) || itemsType === "object" ? {
"type": itemsType,
} : {
"$ref": convertDefinitionPath(itemsType),
};
} else if (memberType == "object") {
property["type"] = memberType;
} else if (memberType === "object") {
property = convertObject(content.content.value);
}

property["description"] = content.hasOwnProperty("meta") && content.meta.hasOwnProperty("description") ?
content.meta.description : "";

properties[memberName] = property;

if (
Expand Down Expand Up @@ -444,6 +444,37 @@ function convertEnum(members) {
return result;
}

function convertObject(object) {
const property = {"type": "object"};
if (!object || !object.hasOwnProperty("content") || !Array.isArray(object.content)) {
return property;
}

const additionalProperties = {};
for (let content of object.content) {
if (
content.hasOwnProperty("content") &&
content.content.hasOwnProperty("key") &&
content.content.key.hasOwnProperty("attributes") &&
content.content.key.attributes.hasOwnProperty("variable") &&
content.content.key.attributes.variable === true &&
content.content.hasOwnProperty("value") &&
content.content.value.hasOwnProperty("element")
) {
const memberType = content.content.value.element;
if (isPrimitiveType(memberType)) {
additionalProperties["type"] = memberType;
} else if (isInheritedType(memberType)) {
additionalProperties["$ref"] = convertDefinitionPath(memberType);
}

property["additionalProperties"] = additionalProperties;
}
}

return property;
}

function convertDefinitionName(name) {
return Strings(name).capitalize().camelize().s;
}
Expand Down

0 comments on commit bab2ba4

Please sign in to comment.