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

Commit 0af89f1

Browse files
committed
chore(oas3): address review comments
1 parent e6c4233 commit 0af89f1

File tree

4 files changed

+57
-47
lines changed

4 files changed

+57
-47
lines changed

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,18 @@ function parseOauthFlowObject(context, object) {
3030
[R.T, parseString(context, scopesName, false)],
3131
]);
3232

33+
const parseScopes = pipeParseResult(namespace,
34+
parseObject(context, scopesName, parseScopeMember, [], true),
35+
scopes => new namespace.elements.Array(scopes.content),
36+
R.map((member) => {
37+
const scope = member.key.clone();
38+
scope.description = member.value;
39+
40+
return scope;
41+
}));
42+
3343
const parseMember = R.cond([
34-
[hasKey('scopes'), R.compose(parseObject(context, scopesName, parseScopeMember, [], true), getValue)],
44+
[hasKey('scopes'), R.compose(parseScopes, getValue)],
3545
[hasKey('refreshUrl'), parseString(context, name, false)],
3646
[hasKey('authorizationUrl'), parseString(context, name, false)],
3747
[hasKey('tokenUrl'), parseString(context, name, false)],
@@ -44,22 +54,7 @@ function parseOauthFlowObject(context, object) {
4454
]);
4555

4656
const parseOauthFlow = pipeParseResult(namespace,
47-
parseObject(context, name, parseMember, requiredKeys, true),
48-
(oauthFlow) => {
49-
const arr = new namespace.elements.Array([]);
50-
const scopes = oauthFlow.get('scopes');
51-
52-
scopes.forEach((value, key) => {
53-
const k = key.clone();
54-
55-
k.description = value;
56-
arr.push(k);
57-
});
58-
59-
oauthFlow.set('scopes', arr);
60-
61-
return oauthFlow;
62-
});
57+
parseObject(context, name, parseMember, requiredKeys, true));
6358

6459
return parseOauthFlow(object);
6560
}

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

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
/* eslint-disable no-unused-vars */
12
const R = require('ramda');
23
const {
3-
isAnnotation, isExtension, hasKey,
4+
isExtension, hasKey, getValue,
45
} = require('../../predicates');
56
const {
67
createWarning,
@@ -12,7 +13,7 @@ const parseOauthFlowObject = require('./parseOauthFlowObject');
1213

1314
const name = 'Oauth Flows Object';
1415

15-
const validFlow = R.anyPass(R.map(hasKey, ['implicit', 'password', 'clientCredentials', 'authorizationCode']));
16+
const isValidFlow = R.anyPass(R.map(hasKey, ['implicit', 'password', 'clientCredentials', 'authorizationCode']));
1617
const grantTypes = {
1718
implicit: 'implicit',
1819
password: 'resource owner password credentials',
@@ -35,27 +36,25 @@ function parseOauthFlowsObject(context, object) {
3536

3637
const parseFlow = (member) => {
3738
const key = member.key.toValue();
38-
const flow = parseOauthFlowObject(context, member.value);
3939

40-
if (flow.length === 0 || isAnnotation(flow.get(0))) {
41-
return flow;
42-
}
40+
const needAuthorizationUrl = flow => R.includes(key, ['implicit', 'authorizationCode']);
41+
const needTokenUrl = flow => R.includes(key, ['password', 'clientCredentials', 'authorizationCode']);
4342

44-
if (['implicit', 'authorizationCode'].includes(key) && !flow.get(0).get('authorizationUrl')) {
45-
return createWarning(namespace,
46-
`'${name}' '${key}' is missing required property 'authorizationUrl'`, member);
47-
}
43+
const hasAuthorizationUrl = flow => flow.get('authorizationUrl');
44+
const hasTokenUrl = flow => flow.get('tokenUrl');
4845

49-
if (['password', 'clientCredentials', 'authorizationCode'].includes(key) && !flow.get(0).get('tokenUrl')) {
50-
return createWarning(namespace,
51-
`'${name}' '${key}' is missing required property 'tokenUrl'`, member);
52-
}
46+
const parse = pipeParseResult(namespace,
47+
R.compose(parseOauthFlowObject(context), getValue),
48+
R.when(R.allPass([R.complement(hasAuthorizationUrl), needAuthorizationUrl]), flow => createWarning(namespace,
49+
`'${name}' '${key}' is missing required property 'authorizationUrl'`, member)),
50+
R.when(R.allPass([R.complement(hasTokenUrl), needTokenUrl]), flow => createWarning(namespace,
51+
`'${name}' '${key}' is missing required property 'tokenUrl'`, member)));
5352

54-
return flow;
53+
return parse(member);
5554
};
5655

5756
const parseMember = R.cond([
58-
[validFlow, parseFlow],
57+
[isValidFlow, parseFlow],
5958

6059
// FIXME Support exposing extensions into parse result
6160
[isExtension, () => new namespace.elements.ParseResult()],
@@ -66,21 +65,16 @@ function parseOauthFlowsObject(context, object) {
6665

6766
const parseOauthFlows = pipeParseResult(namespace,
6867
parseObject(context, name, parseMember),
69-
(oauthFlows) => {
70-
const arr = new namespace.elements.Array();
68+
flows => new namespace.elements.Array(flows.content),
69+
R.map((member) => {
70+
const authScheme = new namespace.elements.AuthScheme();
7171

72-
oauthFlows.forEach((value, key) => {
73-
const authScheme = new namespace.elements.AuthScheme();
72+
authScheme.element = 'Oauth2 Scheme';
73+
authScheme.push(new namespace.elements.Member('grantType', grantTypes[member.key.toValue()]));
74+
authScheme.push(member.value.getMember('scopes'));
7475

75-
authScheme.element = 'Oauth2 Scheme';
76-
authScheme.push(new namespace.elements.Member('grantType', grantTypes[key.toValue()]));
77-
authScheme.push(value.getMember('scopes'));
78-
79-
arr.push(authScheme);
80-
});
81-
82-
return arr;
83-
});
76+
return authScheme;
77+
}));
8478

8579
return parseOauthFlows(object);
8680
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('Oauth Flow Object', () => {
3131
expect(parseResult).to.contain.warning("'Oauth Flow Object' is missing required property 'scopes'");
3232
});
3333

34-
it('provides a warning when scopes is not an object', () => {
34+
it('provides a warning when scopes is not an object', () => {
3535
const oauthFlow = new namespace.elements.Object({
3636
scopes: 1,
3737
});
@@ -148,4 +148,15 @@ describe('Oauth Flow Object', () => {
148148
expect(parseResult.length).to.equal(2);
149149
expect(parseResult).to.contain.warning("'Oauth Flow Object' contains invalid key 'invalid'");
150150
});
151+
152+
it('does not provide warning/errors for extensions', () => {
153+
const oauthFlow = new namespace.elements.Object({
154+
scopes: {},
155+
'x-extension': '',
156+
});
157+
158+
const parseResult = parse(context, oauthFlow);
159+
160+
expect(parseResult).to.not.contain.annotations;
161+
});
151162
});

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,14 @@ describe('Oauth Flows Object', () => {
129129
expect(parseResult.length).to.equal(2);
130130
expect(parseResult).to.contain.warning("'Oauth Flows Object' contains invalid key 'invalid'");
131131
});
132+
133+
it('does not provide warning/errors for extensions', () => {
134+
const oauthFlows = new namespace.elements.Object({
135+
'x-extension': '',
136+
});
137+
138+
const parseResult = parse(context, oauthFlows);
139+
140+
expect(parseResult).to.not.contain.annotations;
141+
});
132142
});

0 commit comments

Comments
 (0)