Skip to content

Parse multiple servers from Open API spec #80

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
59 changes: 37 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ const getEndpointSnippets = function (openApi, path, method, targets, values) {

const hars = OpenAPIToHar.getEndpoint(openApi, path, method, values);

const snippets = [];
let results = [];
for (const har of hars) {
const snippets = [];
const snippet = new HTTPSnippet(har);
snippets.push(
...getSnippetsForTargets(
Expand All @@ -40,17 +41,25 @@ const getEndpointSnippets = function (openApi, path, method, targets, values) {
har.comment ? har.comment : undefined
)
);

// check if multiple content types
let result = results.find((result) => result.url === har.url);
if (result) {
result.snippets.push(...snippets);
continue;
}

// push for multiple servers
results.push({
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
snippets: snippets,
});
}

// use first element since method, url, and description
// are the same for all elements
return {
method: hars[0].method,
url: hars[0].url,
description: hars[0].description,
resource: getResourceName(hars[0].url),
snippets: snippets,
};
return results;
};

/**
Expand All @@ -67,29 +76,35 @@ const getSnippets = function (openApi, targets) {
for (let i in endpointHarInfoList) {
// create HTTPSnippet object:
const harInfo = endpointHarInfoList[i];
const snippets = [];
// create a new harlist array for each endpoint
let harList = [];
for (const har of harInfo.hars) {
const snippets = [];
const snippet = new HTTPSnippet(har);
snippets.push(...getSnippetsForTargets(targets, snippet, har.comment));
harList.push({
method: har.method,
url: har.url,
description: har.description,
resource: getResourceName(har.url),
snippets,
});
}

results.push({
method: harInfo.method,
url: harInfo.url,
description: harInfo.description,
resource: getResourceName(harInfo.url),
snippets,
});
// push snippets of each har corresponding to different servers
results.push(harList);
}

// sort results:
results.sort((a, b) => {
if (a.resource < b.resource) {
if (a[0].resource < b[0].resource) {
return -1;
} else if (a.resource > b.resource) {
} else if (a[0].resource > b[0].resource) {
return 1;
} else {
return getMethodOrder(a.method.toLowerCase(), b.method.toLowerCase());
return getMethodOrder(
a[0].method.toLowerCase(),
b[0].method.toLowerCase()
);
}
});

Expand Down
78 changes: 39 additions & 39 deletions openapi-to-har.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,42 @@ const createHar = function (openApi, path, method, queryParamValues) {
queryParamValues = {};
}

const baseUrl = getBaseUrl(openApi, path, method);

const baseHar = {
method: method.toUpperCase(),
url: baseUrl + getFullPath(openApi, path, method),
headers: getHeadersArray(openApi, path, method),
queryString: getQueryStrings(openApi, path, method, queryParamValues),
httpVersion: 'HTTP/1.1',
cookies: [],
headersSize: 0,
bodySize: 0,
};
const baseUrls = getBaseUrl(openApi, path, method);

let hars = [];

// get payload data, if available:
const postDatas = getPayloads(openApi, path, method);

// For each postData create a snippet
if (postDatas.length > 0) {
for (let i in postDatas) {
const postData = postDatas[i];
const copiedHar = JSON.parse(JSON.stringify(baseHar));
copiedHar.postData = postData;
copiedHar.comment = postData.mimeType;
copiedHar.headers.push({
name: 'content-type',
value: postData.mimeType,
});
hars.push(copiedHar);
baseUrls.forEach((baseUrl) => {
const baseHar = {
method: method.toUpperCase(),
url: baseUrl + getFullPath(openApi, path, method),
headers: getHeadersArray(openApi, path, method),
queryString: getQueryStrings(openApi, path, method, queryParamValues),
httpVersion: 'HTTP/1.1',
cookies: [],
headersSize: 0,
bodySize: 0,
};

// get payload data, if available:
const postDatas = getPayloads(openApi, path, method);

// For each postData create a snippet
if (postDatas.length > 0) {
for (let i in postDatas) {
const postData = postDatas[i];
const copiedHar = JSON.parse(JSON.stringify(baseHar));
copiedHar.postData = postData;
copiedHar.comment = postData.mimeType;
copiedHar.headers.push({
name: 'content-type',
value: postData.mimeType,
});
hars.push(copiedHar);
}
} else {
hars.push(baseHar);
}
} else {
hars = [baseHar];
}
});

return hars;
};
Expand Down Expand Up @@ -193,9 +195,10 @@ const getPayloads = function (openApi, path, method) {
*/
const getBaseUrl = function (openApi, path, method) {
if (openApi.paths[path][method].servers)
return openApi.paths[path][method].servers[0].url;
if (openApi.paths[path].servers) return openApi.paths[path].servers[0].url;
if (openApi.servers) return openApi.servers[0].url;
return openApi.paths[path][method].servers.map((server) => server.url);
if (openApi.paths[path].servers)
return openApi.paths[path].servers.map((server) => server.url);
if (openApi.servers) return openApi.servers.map((server) => server.url);

let baseUrl = '';
if (typeof openApi.schemes !== 'undefined') {
Expand All @@ -210,7 +213,7 @@ const getBaseUrl = function (openApi, path, method) {
baseUrl += '://' + openApi.host + openApi.basePath;
}

return baseUrl;
return [baseUrl];
};

/**
Expand Down Expand Up @@ -503,22 +506,19 @@ const openApiToHarList = function (openApi) {
try {
// iterate openApi and create har objects:
const harList = [];

for (let path in openApi.paths) {
for (let method in openApi.paths[path]) {
const url = getBaseUrl(openApi, path, method) + path;
const hars = createHar(openApi, path, method);
// need to push multiple here
// use hars from createHar for each server endpoint
harList.push({
method: method.toUpperCase(),
url: url,
description:
openApi.paths[path][method].description ||
'No description available',
hars: hars,
});
}
}

return harList;
} catch (e) {
console.log(e);
Expand Down
Loading