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

fix: retrieve base url from spec url in case the servers field is empty #31

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
44 changes: 44 additions & 0 deletions lib/openapi-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,50 @@ OpenApiConnector.prototype.connect = function(cb) {
responseInterceptor: self.connectorHooks.afterExecute,
};

let baseURL, baseURLObject;
if (req.spec.openapi) {
try {
baseURLObject = new URL(self.settings.spec);
baseURL = `${baseURLObject.protocol}//${baseURLObject.host}`;
} catch (error) {
debug('Not a valid URL: %s', error);
}
}

/* servers field in the specification can possibly have the following values:
https://api.example.com
https://api.example.com:8443/v1/reports
http://localhost:3025/v1
http://10.0.81.36/v1
ws://api.example.com/v1
wss://api.example.com/v1
- /v1/reports
- /
- //api.example.com
The last three items are not absoulte paths hence the request fails,
*/

if (baseURL) {
if (!req.spec.servers || !req.spec.servers.length) {
req.spec.servers = [{url: baseURL}];
} else {
req.spec.servers.forEach(function({url}, index) {
try {
// if it successful, its an absoulte path, no changes needed.
new URL(url);
} catch (error) {
// if the url doesn't have the protocol like //api.example.com
if (url.startsWith('//')) {
url = `${baseURLObject.protocol}:${url}`;
} else {
// if the url is a path name like / OR /v1/reports
url = url === '/' ? baseURL : baseURL + url;
}
}
req.spec.servers[index].url = url;
});
}
}
client = await SwaggerClient(req);
if (debug.enabled) {
debug('swagger loaded: %s', self.spec);
Expand Down
8 changes: 8 additions & 0 deletions test/test-connector-openapi3.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const pEvent = require('p-event');

describe('swagger connector for OpenApi 3.0', () => {
let lb4App;
const specUrlWithoutServersField = 'https://restcountries.com/openapi/rest-countries-3.1.yml';
let specUrl = 'http://127.0.0.1:3000/openapi.json';

before(startLB4App);
Expand All @@ -33,6 +34,13 @@ describe('swagger connector for OpenApi 3.0', () => {
});

describe('openapi client generation', () => {
it('generates client from openapi spec url when specs doesnot have servers field', async () => {
const ds = await createDataSource(specUrlWithoutServersField);
ds.connector.should.have.property('client');
ds.connector.client.should.have.property('apis');
ds.connector.api.should.have.property('servers');
});

it('generates client from openapi spec url', async () => {
const ds = await createDataSource(specUrl);
ds.connector.should.have.property('client');
Expand Down
Loading