Skip to content

Commit

Permalink
transform swagger security definition
Browse files Browse the repository at this point in the history
  • Loading branch information
nazieb committed May 23, 2017
1 parent 2c453d5 commit 6b4ac6a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/formatters/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Strings from "string"
import Regex from "re.js"
import moment from "moment"

let securityDefinitions = {};

/**
*
* @param blueprint
Expand All @@ -26,6 +28,8 @@ export default function transformToSwagger(blueprint) {
result["definitions"] = getDefinitions(dataStructures);
}

result["securityDefinitions"] = securityDefinitions;

return result;
}

Expand Down Expand Up @@ -75,6 +79,7 @@ function getActions(actions) {
parameters: [],
responses: {},
operationId: "",
security: [],
};

for (let sample of action.examples) {
Expand Down Expand Up @@ -105,6 +110,7 @@ function getActions(actions) {
}

path.parameters = getActionParams(action);
path.security = getSecurityDefinitions(action);

const method = action.method.toLowerCase();
result[method] = path;
Expand Down Expand Up @@ -278,6 +284,35 @@ function mergeResourceAndActionParams(resourceParams, actionParams) {
return result;
}

function getSecurityDefinitions(action) {
const regexRule = / \{security\:(.*)}/;
let security = [];

if (action.examples.length > 0 && action.examples[0].hasOwnProperty("requests")) {
const request = action.examples[0].requests[0];

for (let header of request.headers) {
const match = header.value.match(regexRule);
if (match != null) {
const definition = {
"in": "header",
"name": header.name,
"type": match[1],
};

const definitionName = header.value.replace(match[0], "");
securityDefinitions[definitionName] = definition;

const actionSecurity = {};
actionSecurity[definitionName] = [];
security.push(actionSecurity);
}
}
}

return security;
}

function getDefinitions(dataStructures) {
const result = {};

Expand Down

0 comments on commit 6b4ac6a

Please sign in to comment.