Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Add better support for when the oas file is missing servers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Oct 2, 2018
1 parent 2958a6b commit 912f9c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 12 additions & 0 deletions packages/api-explorer/__tests__/lib/Oas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ test('should remove end slash from the server URL', () => {
);
});

test('should default missing servers array to example.com', () => {
expect(new Oas({}).servers[0].url).toBe('https://example.com');
});

test('should default empty servers array to example.com', () => {
expect(new Oas({ servers: [] }).servers[0].url).toBe('https://example.com');
});

test('should default empty server object to example.com', () => {
expect(new Oas({ servers: [{}] }).servers[0].url).toBe('https://example.com');
});

describe('operation.getSecurity()', () => {
const security = [{ auth: [] }];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
exports[`operation() should return a default when no operation 1`] = `
Operation {
"method": "get",
"oas": Oas {},
"oas": Oas {
"servers": Array [
Object {
"url": "https://example.com",
},
],
},
"parameters": Array [],
"path": "/unknown",
}
Expand Down
16 changes: 10 additions & 6 deletions packages/api-explorer/src/lib/Oas.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ class Operation {

class Oas {
constructor(oas) {
if (oas.servers) {
let url = oas.servers[0].url;
// Setting up a default server url
oas.servers = oas.servers || [];
oas.servers[0] = oas.servers[0] || {};
oas.servers[0].url = oas.servers[0].url || 'https://example.com';

if (url[url.length - 1] === '/') {
url = url.slice(0, -1);
}
oas.servers[0].url = url;
let url = oas.servers[0].url;
// Stripping the '/' off the end
if (url[url.length - 1] === '/') {
url = url.slice(0, -1);
}

oas.servers[0].url = url;
Object.assign(this, oas);
}

Expand Down

0 comments on commit 912f9c5

Please sign in to comment.