Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit d97db84

Browse files
Marco Frisokylef
authored andcommitted
feat(oas3): add support to 'servers' in 'path item' object
Closes APIARY-6503
1 parent 6d25cd5 commit d97db84

File tree

2 files changed

+78
-15
lines changed

2 files changed

+78
-15
lines changed

packages/fury-adapter-oas3-parser/lib/parser/oas/parsePathItemObject.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ const parseObject = require('../parseObject');
99
const parseString = require('../parseString');
1010
const parseCopy = require('../parseCopy');
1111
const parseParameterObjects = require('./parseParameterObjects');
12+
const parseServersArray = require('./parseServersArray');
1213
const parseOperationObject = require('./parseOperationObject');
1314
const pipeParseResult = require('../../pipeParseResult');
1415

1516
const name = 'Path Item Object';
1617
const httpMethods = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
17-
const unsupportedKeys = ['$ref', 'servers'];
18+
const unsupportedKeys = ['$ref'];
1819

1920
const isHttpMethodKey = R.anyPass(R.map(hasKey, httpMethods));
2021
const isUnsupportedKey = R.anyPass(R.map(hasKey, unsupportedKeys));
@@ -127,6 +128,7 @@ function parsePathItemObject(context, member) {
127128
[hasKey('summary'), parseString(context, name, false)],
128129
[hasKey('description'), parseCopy(context, name, false)],
129130
[hasKey('parameters'), R.curry(parseParameters)(context, member.key)],
131+
[hasKey('servers'), R.compose(parseServersArray(context, name), getValue)],
130132
[isHttpMethodKey, parseOperationObject(context, member.key)],
131133

132134
// FIXME Parse $ref
@@ -150,6 +152,11 @@ function parsePathItemObject(context, member) {
150152
resource.href = hrefFromParameters(member.key, parameters);
151153
resource.hrefVariables = hrefVariablesFromParameters(namespace, parameters);
152154

155+
const hosts = pathItem.get('servers');
156+
if (hosts) {
157+
resource.push(hosts);
158+
}
159+
153160
const summary = pathItem.get('summary');
154161
if (summary) {
155162
resource.title = summary.clone();

packages/fury-adapter-oas3-parser/test/unit/parser/oas/parsePathItemObject-test.js

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,6 @@ describe('Path Item Object', () => {
6868
expect(parseResult).to.contain.warning("'Path Item Object' contains unsupported key '$ref'");
6969
});
7070

71-
it('warns for a servers', () => {
72-
const path = new namespace.elements.Member('/', {
73-
servers: '',
74-
});
75-
76-
const parseResult = parse(context, path);
77-
78-
expect(parseResult.length).to.equal(2);
79-
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Resource);
80-
expect(parseResult.get(0).href.toValue()).to.equal('/');
81-
82-
expect(parseResult).to.contain.warning("'Path Item Object' contains unsupported key 'servers'");
83-
});
84-
8571
it('does not provide warning for Info Object extensions', () => {
8672
const path = new namespace.elements.Member('/', {
8773
'x-extension': '',
@@ -419,4 +405,74 @@ describe('Path Item Object', () => {
419405
});
420406
});
421407
});
408+
409+
describe('#servers', () => {
410+
it('warns when servers is not an array', () => {
411+
const path = new namespace.elements.Member('/', {
412+
servers: {},
413+
});
414+
415+
const parseResult = parse(context, path);
416+
417+
expect(parseResult.length).to.equal(2);
418+
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Resource);
419+
expect(parseResult.get(0).href.toValue()).to.equal('/');
420+
421+
expect(parseResult).to.contain.warning("'Path Item Object' 'servers' is not an array");
422+
});
423+
424+
it('exposes servers as an array in the resource', () => {
425+
const path = new namespace.elements.Member('/', {
426+
servers: [
427+
{
428+
url: 'https://{username}.server.com/1.0',
429+
variables: {
430+
username: {
431+
default: 'Mario',
432+
enum: ['Tony', 'Nina'],
433+
},
434+
},
435+
},
436+
{
437+
url: 'https://user.server.com/2.0',
438+
description: 'The production API server',
439+
},
440+
],
441+
});
442+
443+
const parseResult = parse(context, path);
444+
445+
expect(parseResult.length).to.equal(1);
446+
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Resource);
447+
448+
const hostsCategory = parseResult.get(0).get(0);
449+
expect(hostsCategory).to.be.instanceof(namespace.elements.Category);
450+
expect(hostsCategory.classes.toValue()).to.deep.equal(['hosts']);
451+
expect(hostsCategory.length).to.equal(2);
452+
453+
const firstHost = hostsCategory.get(0);
454+
expect(firstHost).to.be.instanceof(namespace.elements.Resource);
455+
expect(firstHost.href.toValue()).to.equal('https://{username}.server.com/1.0');
456+
457+
const { hrefVariables } = firstHost;
458+
expect(hrefVariables).to.be.instanceof(namespace.elements.HrefVariables);
459+
expect(hrefVariables.length).to.equal(1);
460+
461+
const hrefVariable = hrefVariables.content.content[0];
462+
expect(hrefVariable).to.be.instanceof(namespace.elements.Member);
463+
expect(hrefVariable.key.toValue()).to.equal('username');
464+
expect(hrefVariable.value.default).to.equal('Mario');
465+
466+
const { enumerations } = hrefVariable.value;
467+
expect(enumerations).to.be.instanceof(namespace.elements.Array);
468+
expect(enumerations.length).to.equal(2);
469+
expect(enumerations.toValue()).to.deep.equal(['Tony', 'Nina']);
470+
471+
const secondHost = hostsCategory.get(1);
472+
expect(secondHost).to.be.instanceof(namespace.elements.Resource);
473+
expect(secondHost.classes.toValue()).to.deep.equal(['host']);
474+
expect(secondHost.description.toValue()).to.equal('The production API server');
475+
expect(secondHost.href.toValue()).to.equal('https://user.server.com/2.0');
476+
});
477+
});
422478
});

0 commit comments

Comments
 (0)