Skip to content

do variable substitutions in server urls #103

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 1 commit 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
11 changes: 11 additions & 0 deletions openapi-to-har.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,17 @@ 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 && openApi.servers.length > 0) {
let firstServer = openApi.servers[0];
let url = firstServer.url;
const variables = firstServer.variables || {};
Object.keys(variables).forEach((variable) => {
url = url.replace(`{${variable}}`, variables[variable].default);
});
return url;
}

if (openApi.servers) return openApi.servers[0].url;

let baseUrl = '';
Expand Down
7 changes: 6 additions & 1 deletion test/form_data_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
},
"servers": [
{
"url": "http://petstore.swagger.io/api"
"url": "http://{hostname}/api",
"variables": {
"hostname": {
"default": "petstore.swagger.io"
}
}
}
],
"paths": {
Expand Down
15 changes: 15 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ test('Generate snippet with multipart/form-data', function (t) {
t.end();
});

test('If a server has url with variables those should be substituted', function (t) {
const result = OpenAPISnippets.getEndpointSnippets(
FormDataExampleReferenceAPI,
'/pets',
'patch',
['csharp_restsharp']
);
const snippet = result.snippets[0].content;
t.true(
/new RestClient\("http:\/\/petstore.swagger.io\/api\/pets"/.test(snippet)
);
t.false(/{hostname}/.test(snippet));
t.end();
});

test('Generate snippet with multipart/form-data with array', function (t) {
const result = OpenAPISnippets.getEndpointSnippets(
FormDataExampleReferenceAPI,
Expand Down