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

Commit c5b4643

Browse files
authored
Merge pull request #360 from apiaryio/kylef/oas3-license
Add support for License Object
2 parents 7f032c6 + 039526d commit c5b4643

File tree

8 files changed

+261
-135
lines changed

8 files changed

+261
-135
lines changed

packages/fury-adapter-oas3-parser/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Fury OAS3 Parser Changelog
22

3+
## Master
4+
5+
### Enhancements
6+
7+
- Added support for `info.license` (License Object).
8+
39
## 0.9.1 (2019-08-08)
410

511
### Enhancements

packages/fury-adapter-oas3-parser/STATUS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Key:
3232
| description ||
3333
| termsOfService | [](https://github.com/apiaryio/api-elements.js/issues/78) |
3434
| contact | [](https://github.com/apiaryio/api-elements.js/issues/79) |
35-
| license | [](https://github.com/apiaryio/api-elements.js/issues/80) |
35+
| license | |
3636
| version ||
3737

3838
## Paths Object

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ const {
55
createInvalidMemberWarning,
66
} = require('../annotations');
77
const {
8-
isObject, hasKey, isExtension,
8+
isObject, hasKey, getValue, isExtension,
99
} = require('../../predicates');
1010
const parseObject = require('../parseObject');
1111
const parseString = require('../parseString');
1212
const parseCopy = require('../parseCopy');
1313
const pipeParseResult = require('../../pipeParseResult');
14+
const parseLicenseObject = require('./parseLicenseObject');
1415

1516
const name = 'Info Object';
1617
const requiredKeys = ['title', 'version'];
17-
const unsupportedKeys = ['termsOfService', 'contact', 'license'];
18+
const unsupportedKeys = ['termsOfService', 'contact'];
1819

1920
/**
2021
* Returns whether the given member element is unsupported
@@ -38,6 +39,7 @@ function parseInfo(context, info) {
3839
[hasKey('title'), parseString(context, name, true)],
3940
[hasKey('version'), parseString(context, name, true)],
4041
[hasKey('description'), parseCopy(context, name, false)],
42+
[hasKey('license'), R.compose(parseLicenseObject(context), getValue)],
4143
[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],
4244

4345
// FIXME Support exposing extensions into parse result
@@ -60,6 +62,10 @@ function parseInfo(context, info) {
6062
api.push(info.get('description'));
6163
}
6264

65+
if (info.get('license')) {
66+
api.links.push(info.get('license'));
67+
}
68+
6369
return api;
6470
});
6571

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
const R = require('ramda');
3+
const { createWarning } = require('../../elements');
4+
const {
5+
createInvalidMemberWarning,
6+
} = require('../annotations');
7+
const {
8+
isObject, hasKey, isExtension,
9+
} = require('../../predicates');
10+
const parseObject = require('../parseObject');
11+
const parseString = require('../parseString');
12+
const pipeParseResult = require('../../pipeParseResult');
13+
14+
const name = 'License Object';
15+
const requiredKeys = ['name'];
16+
17+
const parseMember = context => R.cond([
18+
[hasKey('name'), parseString(context, name, false)],
19+
[hasKey('url'), parseString(context, name, false)],
20+
[isExtension, () => new context.namespace.elements.ParseResult()],
21+
[R.T, createInvalidMemberWarning(context.namespace, name)],
22+
]);
23+
24+
/**
25+
* Parse the OpenAPI 'License Object' (`#/info/license`)
26+
* @see http://spec.openapis.org/oas/v3.0.2#license-object
27+
* @returns ParseResult<Link>
28+
* @private
29+
*/
30+
const parseLicenseObject = context => pipeParseResult(context.namespace,
31+
R.unless(isObject, createWarning(context.namespace, `'${name}' is not an object`)),
32+
parseObject(context, name, parseMember(context), requiredKeys, [], true),
33+
(object) => {
34+
const link = new context.namespace.elements.Link();
35+
link.relation = 'license';
36+
link.title = object.get('name');
37+
link.href = object.getValue('url') || 'http://purl.org/atompub/license#unspecified';
38+
return link;
39+
});
40+
41+
module.exports = parseLicenseObject;

packages/fury-adapter-oas3-parser/test/integration/fixtures/petstore.json

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@
1616
"title": {
1717
"element": "string",
1818
"content": "Swagger Petstore"
19+
},
20+
"links": {
21+
"element": "array",
22+
"content": [
23+
{
24+
"element": "link",
25+
"meta": {
26+
"title": {
27+
"element": "string",
28+
"content": "MIT"
29+
}
30+
},
31+
"attributes": {
32+
"relation": {
33+
"element": "string",
34+
"content": "license"
35+
},
36+
"href": {
37+
"element": "string",
38+
"content": "http://purl.org/atompub/license#unspecified"
39+
}
40+
}
41+
}
42+
]
1943
}
2044
},
2145
"attributes": {
@@ -527,66 +551,6 @@
527551
}
528552
]
529553
},
530-
{
531-
"element": "annotation",
532-
"meta": {
533-
"classes": {
534-
"element": "array",
535-
"content": [
536-
{
537-
"element": "string",
538-
"content": "warning"
539-
}
540-
]
541-
}
542-
},
543-
"attributes": {
544-
"sourceMap": {
545-
"element": "array",
546-
"content": [
547-
{
548-
"element": "sourceMap",
549-
"content": [
550-
{
551-
"element": "array",
552-
"content": [
553-
{
554-
"element": "number",
555-
"attributes": {
556-
"line": {
557-
"element": "number",
558-
"content": 5
559-
},
560-
"column": {
561-
"element": "number",
562-
"content": 3
563-
}
564-
},
565-
"content": 68
566-
},
567-
{
568-
"element": "number",
569-
"attributes": {
570-
"line": {
571-
"element": "number",
572-
"content": 5
573-
},
574-
"column": {
575-
"element": "number",
576-
"content": 10
577-
}
578-
},
579-
"content": 7
580-
}
581-
]
582-
}
583-
]
584-
}
585-
]
586-
}
587-
},
588-
"content": "'Info Object' contains unsupported key 'license'"
589-
},
590554
{
591555
"element": "annotation",
592556
"meta": {

packages/fury-adapter-oas3-parser/test/integration/fixtures/petstore.sourcemap.json

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,55 @@
4141
}
4242
},
4343
"content": "Swagger Petstore"
44+
},
45+
"links": {
46+
"element": "array",
47+
"content": [
48+
{
49+
"element": "link",
50+
"meta": {
51+
"title": {
52+
"element": "string",
53+
"attributes": {
54+
"sourceMap": {
55+
"element": "array",
56+
"content": [
57+
{
58+
"element": "sourceMap",
59+
"content": [
60+
{
61+
"element": "array",
62+
"content": [
63+
{
64+
"element": "number",
65+
"content": 87
66+
},
67+
{
68+
"element": "number",
69+
"content": 3
70+
}
71+
]
72+
}
73+
]
74+
}
75+
]
76+
}
77+
},
78+
"content": "MIT"
79+
}
80+
},
81+
"attributes": {
82+
"relation": {
83+
"element": "string",
84+
"content": "license"
85+
},
86+
"href": {
87+
"element": "string",
88+
"content": "http://purl.org/atompub/license#unspecified"
89+
}
90+
}
91+
}
92+
]
4493
}
4594
},
4695
"attributes": {
@@ -1277,66 +1326,6 @@
12771326
}
12781327
]
12791328
},
1280-
{
1281-
"element": "annotation",
1282-
"meta": {
1283-
"classes": {
1284-
"element": "array",
1285-
"content": [
1286-
{
1287-
"element": "string",
1288-
"content": "warning"
1289-
}
1290-
]
1291-
}
1292-
},
1293-
"attributes": {
1294-
"sourceMap": {
1295-
"element": "array",
1296-
"content": [
1297-
{
1298-
"element": "sourceMap",
1299-
"content": [
1300-
{
1301-
"element": "array",
1302-
"content": [
1303-
{
1304-
"element": "number",
1305-
"attributes": {
1306-
"line": {
1307-
"element": "number",
1308-
"content": 5
1309-
},
1310-
"column": {
1311-
"element": "number",
1312-
"content": 3
1313-
}
1314-
},
1315-
"content": 68
1316-
},
1317-
{
1318-
"element": "number",
1319-
"attributes": {
1320-
"line": {
1321-
"element": "number",
1322-
"content": 5
1323-
},
1324-
"column": {
1325-
"element": "number",
1326-
"content": 10
1327-
}
1328-
},
1329-
"content": 7
1330-
}
1331-
]
1332-
}
1333-
]
1334-
}
1335-
]
1336-
}
1337-
},
1338-
"content": "'Info Object' contains unsupported key 'license'"
1339-
},
13401329
{
13411330
"element": "annotation",
13421331
"meta": {

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,6 @@ describe('#parseInfoObject', () => {
111111
expect(parseResult).to.contain.warning("'Info Object' contains unsupported key 'contact'");
112112
});
113113

114-
it('provides warning for unsupported license key', () => {
115-
const object = new namespace.elements.Object({
116-
title: 'My API',
117-
version: '1.0.0',
118-
license: {},
119-
});
120-
121-
const parseResult = parse(context, object);
122-
123-
expect(parseResult).to.contain.warning("'Info Object' contains unsupported key 'license'");
124-
});
125-
126114
it('does not provide warning for Info Object extensions', () => {
127115
const object = new namespace.elements.Object({
128116
title: 'My API',
@@ -172,4 +160,22 @@ describe('#parseInfoObject', () => {
172160
expect(parseResult.length).to.equal(1);
173161
expect(parseResult.api.copy.toValue()).to.deep.equal(['My API Description']);
174162
});
163+
164+
it('provides api category with license', () => {
165+
const info = new namespace.elements.Object({
166+
title: 'My API',
167+
version: '1.0.0',
168+
license: {
169+
name: 'Apache 2.0',
170+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html',
171+
},
172+
});
173+
174+
const parseResult = parse(context, info);
175+
expect(parseResult.length).to.equal(1);
176+
177+
const license = parseResult.api.links.get(0);
178+
expect(license.relation.toValue()).to.equal('license');
179+
expect(license.href.toValue()).to.equal('https://www.apache.org/licenses/LICENSE-2.0.html');
180+
});
175181
});

0 commit comments

Comments
 (0)