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

Commit c07d897

Browse files
author
Marco Friso
committed
fix(oas3): refactors, add test, remove file
1 parent 81e34fc commit c07d897

7 files changed

Lines changed: 58 additions & 118 deletions

File tree

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

Lines changed: 0 additions & 57 deletions
This file was deleted.

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ function parseOASObject(context, object) {
144144
}
145145

146146
if (hosts) {
147-
if (!hosts.isEmpty) {
148-
api.push(new namespace.elements.Category(
149-
hosts.content, { classes: ['hosts'] }
150-
));
151-
}
147+
api.push(hosts);
152148
}
153149

154150
const resources = object.get('paths');

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ const requiredKeys = ['url'];
1616
const parseMember = context => R.cond([
1717
[hasKey('description'), parseString(context, name, false)],
1818
[hasKey('url'), parseString(context, name, true)],
19-
// [hasKey('variables'), R.compose(parseServerVariablesObject(context), getValue)], // NOT SUPPORTED YET
20-
[hasKey('variables'), createUnsupportedMemberWarning(context.namespace, name)],
19+
[hasKey('variables'), createUnsupportedMemberWarning(context.namespace, name)], // NOT SUPPORTED YET
2120
[isExtension, () => new context.namespace.elements.ParseResult()],
2221
[R.T, createInvalidMemberWarning(context.namespace, name)],
2322
]);
2423

2524
/**
2625
* Parse the OpenAPI 'Server Object' (`#/server`)
2726
* @see http://spec.openapis.org/oas/v3.0.3#server-object
28-
* @returns ParseResult<Link>
27+
* @returns ParseResult<Resource>
2928
* @private
3029
*/
3130
const parseServerObject = context => pipeParseResult(context.namespace,
@@ -37,11 +36,10 @@ const parseServerObject = context => pipeParseResult(context.namespace,
3736
resource.classes.push('host');
3837

3938
if (object.hasKey('description')) {
40-
const description = object.getValue('description');
41-
resource.description = description;
39+
resource.description = object.get('description');
4240
}
4341

44-
resource.href = object.getValue('url');
42+
resource.href = object.get('url');
4543

4644
return resource;
4745
});

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ function parseServersArray(context, element) {
1818
const { namespace } = context;
1919

2020
const parseServers = pipeParseResult(namespace,
21-
parseArray(context, name, R.curry(parseServerObject)(context)));
21+
parseArray(context, name, R.curry(parseServerObject)(context)),
22+
array => new namespace.elements.Category(
23+
array.content, { classes: ['hosts'] }
24+
));
25+
2226
return parseServers(element);
2327
}
2428

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ describe('#parseOpenAPIObject', () => {
7272
expect(parseResult.api.title.toValue()).to.equal('My API');
7373
expect(parseResult.api.length).to.equal(1);
7474

75-
const serversArray = parseResult.api.get(0);
76-
expect(serversArray).to.be.instanceof(namespace.elements.Category);
77-
expect(serversArray.classes.toValue()).to.deep.equal(['hosts']);
78-
expect(serversArray.length).to.equal(2);
75+
const hostsCategory = parseResult.api.get(0);
76+
expect(hostsCategory).to.be.instanceof(namespace.elements.Category);
77+
expect(hostsCategory.classes.toValue()).to.deep.equal(['hosts']);
78+
expect(hostsCategory.length).to.equal(2);
7979

80-
const firstServer = serversArray.get(0);
80+
const firstServer = hostsCategory.get(0);
8181
expect(firstServer).to.be.instanceof(namespace.elements.Resource);
82-
expect(firstServer.attributes.content[0].toValue().value).to.equal('https://user.server.com/1.0');
82+
expect(firstServer.href.toValue()).to.equal('https://user.server.com/1.0');
8383
});
8484

8585
describe('with schema components', () => {
@@ -219,22 +219,6 @@ describe('#parseOpenAPIObject', () => {
219219
expect(parseResult).to.contain.warning("'OpenAPI Object' contains unsupported key 'externalDocs'");
220220
});
221221

222-
it('provides warning for unsupported servers key', () => {
223-
const object = new namespace.elements.Object({
224-
openapi: '3.0.0',
225-
info: {
226-
title: 'My API',
227-
version: '1.0.0',
228-
},
229-
paths: {},
230-
servers: [],
231-
});
232-
233-
const parseResult = parse(context, object);
234-
235-
expect(parseResult).to.contain.warning("'OpenAPI Object' contains unsupported key 'servers'");
236-
});
237-
238222
it('provides warning for invalid keys', () => {
239223
const object = new namespace.elements.Object({
240224
openapi: '3.0.0',

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ describe('#parseHostsObject', () => {
2222
});
2323

2424
describe('#url', () => {
25+
it('warns when server object does not contain URL', () => {
26+
const server = new namespace.elements.Object({
27+
});
28+
29+
const parseResult = parse(context)(server);
30+
expect(parseResult.length).to.equal(1);
31+
expect(parseResult).to.contain.warning("'Server Object' is missing required property 'url'");
32+
});
33+
34+
it('warns when URL is not a string', () => {
35+
const server = new namespace.elements.Object({
36+
url: 1234,
37+
description: 'The production API server',
38+
});
39+
40+
const parseResult = parse(context)(server);
41+
expect(parseResult).to.contain.annotations;
42+
expect(parseResult).to.contain.error("'Server Object' 'url' is not a string");
43+
});
44+
2545
it('parse server object with URL', () => {
2646
const server = new namespace.elements.Object({
2747
url: 'https://{username}.gigantic-server.com/{version}',
@@ -38,15 +58,6 @@ describe('#parseHostsObject', () => {
3858
const href = resource.href.toValue();
3959
expect(href).to.be.equal('https://{username}.gigantic-server.com/{version}');
4060
});
41-
42-
it('warns when server object does not contain URL', () => {
43-
const server = new namespace.elements.Object({
44-
});
45-
46-
const parseResult = parse(context)(server);
47-
expect(parseResult.length).to.equal(1);
48-
expect(parseResult).to.contain.warning("'Server Object' is missing required property 'url'");
49-
});
5061
});
5162

5263
describe('#description', () => {
@@ -58,6 +69,7 @@ describe('#parseHostsObject', () => {
5869

5970
const parseResult = parse(context)(server);
6071
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Resource);
72+
expect(parseResult).to.contain.annotations;
6173
expect(parseResult).to.contain.warning("'Server Object' 'description' is not a string");
6274
});
6375

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Context = require('../../../../lib/context');
55

66
const { minim: namespace } = new Fury();
77

8-
describe('Servers Array', () => {
8+
describe('#parseHostsObject', () => {
99
let context;
1010

1111
beforeEach(() => {
@@ -33,14 +33,15 @@ describe('Servers Array', () => {
3333

3434
expect(parseResult.length).to.equal(1);
3535

36-
const servers = parseResult.get(0);
36+
const hosts = parseResult.get(0);
37+
const host = hosts.get(0);
3738

38-
expect(servers).to.be.instanceof(namespace.elements.Array);
39-
expect(servers.length).to.equal(1);
40-
expect(servers.get(0)).to.be.instanceof(namespace.elements.Resource);
41-
expect(servers.get(0).meta.content[0].content.value.content[0].content).to.equal('host');
42-
expect(servers.get(0).meta.content[1].content.value.content).to.equal('The production API server');
43-
expect(servers.get(0).attributes.content[0].content.value.content).to.equal('https://user.server.com/1.0');
39+
expect(hosts).to.be.instanceof(namespace.elements.Array);
40+
expect(hosts.length).to.equal(1);
41+
expect(host).to.be.instanceof(namespace.elements.Resource);
42+
expect(host.classes.toValue()).to.deep.equal(['host']);
43+
expect(host.description.toValue()).to.equal('The production API server');
44+
expect(host.href.toValue()).to.equal('https://user.server.com/1.0');
4445
});
4546

4647
it('parses correctly when there are multiple servers', () => {
@@ -58,17 +59,19 @@ describe('Servers Array', () => {
5859

5960
expect(parseResult.length).to.equal(1);
6061

61-
const servers = parseResult.get(0);
62+
const hosts = parseResult.get(0);
63+
const firstHost = hosts.get(0);
64+
const secondHost = hosts.get(1);
6265

63-
expect(servers).to.be.instanceof(namespace.elements.Array);
64-
expect(servers.length).to.equal(2);
65-
expect(servers.get(0)).to.be.instanceof(namespace.elements.Resource);
66-
expect(servers.get(0).meta.content[0].content.value.content[0].content).to.equal('host');
67-
expect(servers.get(0).attributes.content[0].content.value.content).to.equal('https://user.server.com/1.0');
66+
expect(hosts).to.be.instanceof(namespace.elements.Array);
67+
expect(hosts.length).to.equal(2);
68+
expect(firstHost).to.be.instanceof(namespace.elements.Resource);
69+
expect(firstHost.classes.toValue()).to.deep.equal(['host']);
70+
expect(firstHost.href.toValue()).to.equal('https://user.server.com/1.0');
6871

69-
expect(servers.get(1)).to.be.instanceof(namespace.elements.Resource);
70-
expect(servers.get(1).meta.content[0].content.value.content[0].content).to.equal('host');
71-
expect(servers.get(1).meta.content[1].content.value.content).to.equal('The production API server');
72-
expect(servers.get(1).attributes.content[0].content.value.content).to.equal('https://user.server.com/2.0');
72+
expect(secondHost).to.be.instanceof(namespace.elements.Resource);
73+
expect(secondHost.classes.toValue()).to.deep.equal(['host']);
74+
expect(secondHost.description.toValue()).to.equal('The production API server');
75+
expect(secondHost.href.toValue()).to.equal('https://user.server.com/2.0');
7376
});
7477
});

0 commit comments

Comments
 (0)