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

Commit 0497bfc

Browse files
committed
fix(oas3): attach description to hrefVariables' member
1 parent 9b41de9 commit 0497bfc

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

packages/openapi3-parser/lib/parser/oas/parseServerVariableObject.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ const parseMember = context => R.cond([
5050
* @returns ParseResult<Member>
5151
* @private
5252
*/
53-
const parseServerVariableObject = (context, element) => pipeParseResult(context.namespace,
53+
const parseServerVariableObject = (context, element, variableName) => pipeParseResult(context.namespace,
5454
R.unless(isObject, createWarning(context.namespace, `'${name}' is not an object`)),
5555
parseObject(context, name, parseMember(context), requiredKeys, [], true),
5656
(object) => {
5757
const variable = R.or(object.get('enum'), new context.namespace.elements.String());
5858

5959
variable.attributes.set('default', object.get('default'));
6060

61+
const member = new context.namespace.elements.Member(variableName, variable);
62+
6163
if (object.hasKey('description')) {
62-
variable.description = object.getValue('description');
64+
member.description = object.getValue('description');
6365
}
6466

65-
return variable;
67+
return member;
6668
})(element);
6769

6870
module.exports = R.curry(parseServerVariableObject);

packages/openapi3-parser/lib/parser/parseMap.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const R = require('ramda');
22
const pipeParseResult = require('../pipeParseResult');
33
const {
4-
isObject, getValue, isAnnotation,
4+
isObject, getValue, isAnnotation, isMember,
55
} = require('../predicates');
66
const { createWarning } = require('./annotations');
77
const parseObject = require('./parseObject');
88

9+
const isAnnotationOrMember = R.anyPass([isAnnotation, isMember]);
10+
911
const validateMapValueIsObject = (namespace, name, key) => R.unless(R.pipe(getValue, isObject), createWarning(namespace, `'${name}' '${key}' is not an object`));
1012

1113
const isParseResultEmpty = parseResult => R.reject(isAnnotation, parseResult).isEmpty;
@@ -15,8 +17,8 @@ const parseMapMember = R.curry((context, parser, member) => {
1517
const Member = R.constructN(2, context.namespace.elements.Member)(member.key);
1618

1719
const parseResult = R.map(
18-
R.unless(isAnnotation, Member),
19-
parser(context, member.value)
20+
R.unless(isAnnotationOrMember, Member),
21+
parser(context, member.value, member.key)
2022
);
2123

2224
if (isParseResultEmpty(parseResult)) {

packages/openapi3-parser/test/unit/parser/oas/parseServerObject-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('#parseServerObject', () => {
137137
expect(firstHrefVariable).to.be.instanceof(namespace.elements.Member);
138138
expect(firstHrefVariable.key.toValue()).to.equal('username');
139139
expect(firstHrefVariable.value.attributes.get('default').toValue()).to.equal('Mario');
140-
expect(firstHrefVariable.value.description.toValue()).to.equal('API user name');
140+
expect(firstHrefVariable.description.toValue()).to.equal('API user name');
141141

142142
expect(secondHrefVariable).to.be.instanceof(namespace.elements.Member);
143143
expect(secondHrefVariable.key.toValue()).to.equal('version');
@@ -196,7 +196,7 @@ describe('#parseServerObject', () => {
196196
expect(firstHrefVariable).to.be.instanceof(namespace.elements.Member);
197197
expect(firstHrefVariable.key.toValue()).to.equal('username');
198198
expect(firstHrefVariable.value.attributes.get('default').toValue()).to.equal('Mario');
199-
expect(firstHrefVariable.value.description.toValue()).to.equal('API user name');
199+
expect(firstHrefVariable.description.toValue()).to.equal('API user name');
200200

201201
expect(secondHrefVariable).to.be.instanceof(namespace.elements.Member);
202202
expect(secondHrefVariable.key.toValue()).to.equal('version');
@@ -234,7 +234,7 @@ describe('#parseServerObject', () => {
234234
expect(firstHrefVariable).to.be.instanceof(namespace.elements.Member);
235235
expect(firstHrefVariable.key.toValue()).to.equal('username');
236236
expect(firstHrefVariable.value.attributes.get('default').toValue()).to.equal('Mario');
237-
expect(firstHrefVariable.value.description.toValue()).to.equal('API user name');
237+
expect(firstHrefVariable.description.toValue()).to.equal('API user name');
238238

239239
expect(secondHrefVariable).to.be.instanceof(namespace.elements.Member);
240240
expect(secondHrefVariable.key.toValue()).to.equal('version');

packages/openapi3-parser/test/unit/parser/oas/parseServerVariableObject-test.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('#parseServerVariableObject', () => {
1515
it('provides warning when server variable is non-object', () => {
1616
const serverVariable = new namespace.elements.String();
1717

18-
const parseResult = parse(context)(serverVariable);
18+
const parseResult = parse(context)(serverVariable, 'name');
1919

2020
expect(parseResult.length).to.equal(1);
2121
expect(parseResult).to.contain.warning("'Server Variable Object' is not an object");
@@ -26,7 +26,7 @@ describe('#parseServerVariableObject', () => {
2626
const serverVariable = new namespace.elements.Object({
2727
});
2828

29-
const parseResult = parse(context)(serverVariable);
29+
const parseResult = parse(context)(serverVariable, 'name');
3030
expect(parseResult.length).to.equal(1);
3131
expect(parseResult).to.contain.warning("'Server Variable Object' is missing required property 'default'");
3232
});
@@ -37,7 +37,7 @@ describe('#parseServerVariableObject', () => {
3737
description: 'API user name',
3838
});
3939

40-
const parseResult = parse(context)(serverVariable);
40+
const parseResult = parse(context)(serverVariable, 'name');
4141
expect(parseResult).to.contain.error("'Server Variable Object' 'default' is not a string");
4242
});
4343

@@ -46,12 +46,13 @@ describe('#parseServerVariableObject', () => {
4646
default: 'Mario',
4747
});
4848

49-
const parseResult = parse(context)(serverVariable);
49+
const parseResult = parse(context)(serverVariable, 'name');
5050
expect(parseResult).to.not.contain.annotations;
5151

5252
const member = parseResult.get(0);
53-
expect(member).to.be.instanceof(namespace.elements.String);
54-
expect(member.attributes.get('default').toValue()).to.be.equal('Mario');
53+
expect(member).to.be.instanceof(namespace.elements.Member);
54+
expect(member.value).to.be.instanceof(namespace.elements.String);
55+
expect(member.value.attributes.get('default').toValue()).to.be.equal('Mario');
5556
});
5657
});
5758

@@ -62,22 +63,24 @@ describe('#parseServerVariableObject', () => {
6263
description: 1234,
6364
});
6465

65-
const parseResult = parse(context)(serverVariable);
66-
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.String);
66+
const parseResult = parse(context)(serverVariable, 'name');
67+
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Member);
68+
expect(parseResult.get(0).value).to.be.instanceof(namespace.elements.String);
69+
expect(parseResult.get(1)).to.be.instanceof(namespace.elements.Annotation);
6770
expect(parseResult).to.contain.warning("'Server Variable Object' 'description' is not a string");
6871
});
6972

70-
it('parse server variable object with description', () => {
73+
it('attaches description to member', () => {
7174
const serverVariable = new namespace.elements.Object({
7275
default: 'Mario',
7376
description: 'API user name',
7477
});
7578

76-
const parseResult = parse(context)(serverVariable);
79+
const parseResult = parse(context)(serverVariable, 'name');
7780
expect(parseResult).to.not.contain.annotations;
7881

7982
const member = parseResult.get(0);
80-
expect(member).to.be.instanceof(namespace.elements.String);
83+
expect(member).to.be.instanceof(namespace.elements.Member);
8184

8285
const description = member.description.toValue();
8386
expect(description).to.be.equal('API user name');
@@ -90,7 +93,7 @@ describe('#parseServerVariableObject', () => {
9093
default: 'Mario',
9194
enum: 1,
9295
});
93-
const parseResult = parse(context)(serverVariable);
96+
const parseResult = parse(context)(serverVariable, 'name');
9497

9598
expect(parseResult).to.contain.warning(
9699
"'Server Variable Object' 'enum' is not an array"
@@ -103,13 +106,14 @@ describe('#parseServerVariableObject', () => {
103106
enum: ['Tony', 'Nina'],
104107
});
105108

106-
const parseResult = parse(context)(serverVariable);
109+
const parseResult = parse(context)(serverVariable, 'name');
107110
expect(parseResult).to.not.contain.annotations;
108111

109-
const enumElement = parseResult.get(0);
110-
expect(enumElement).to.be.instanceof(namespace.elements.Enum);
112+
const member = parseResult.get(0);
113+
expect(member).to.be.instanceof(namespace.elements.Member);
114+
expect(member.value).to.be.instanceof(namespace.elements.Enum);
111115

112-
const { enumerations } = enumElement;
116+
const { enumerations } = member.value;
113117
expect(enumerations).to.be.instanceof(namespace.elements.Array);
114118
expect(enumerations.length).to.equal(2);
115119
expect(enumerations.toValue()).to.deep.equal(['Tony', 'Nina']);
@@ -125,17 +129,18 @@ describe('#parseServerVariableObject', () => {
125129
enum: ['Tony', 1, 'Nina', true],
126130
});
127131

128-
const parseResult = parse(context)(serverVariable);
132+
const parseResult = parse(context)(serverVariable, 'name');
129133
expect(parseResult).to.contain.annotations;
130134

131135
const { annotations } = parseResult;
132136
expect(annotations.length).to.equal(2);
133137
expect(annotations.get(0).toValue()).to.deep.equal("'Server Variable Object' 'enum' array value is not a string");
134138

135-
const enumElement = parseResult.get(0);
136-
expect(enumElement).to.be.instanceof(namespace.elements.Enum);
139+
const member = parseResult.get(0);
140+
expect(member).to.be.instanceof(namespace.elements.Member);
141+
expect(member.value).to.be.instanceof(namespace.elements.Enum);
137142

138-
const { enumerations } = enumElement;
143+
const { enumerations } = member.value;
139144
expect(enumerations).to.be.instanceof(namespace.elements.Array);
140145
expect(enumerations.length).to.equal(2);
141146
expect(enumerations.toValue()).to.deep.equal(['Tony', 'Nina']);

0 commit comments

Comments
 (0)