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

Commit 0288e87

Browse files
committed
fix(oas2): add optional typeAttribute for optional parameters
1 parent ed1d127 commit 0288e87

File tree

10 files changed

+128
-1
lines changed

10 files changed

+128
-1
lines changed

packages/fury-adapter-swagger/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Prevents a 'Path Item Object' from being included in a Resource Group created
88
by an 'Operation Object' in a previously defined 'Path Item Object'.
99

10+
- Optional parameters will now include an optional typeAttribute in the parse
11+
result. This will fix conversion to API Blueprint with fury-cli where
12+
optional parameters have shown up as required in the generated API Blueprint.
13+
1014
## 0.27.1 (2019-06-03)
1115

1216
### Bug Fixes

packages/fury-adapter-swagger/lib/parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,8 @@ class Parser {
14451445

14461446
if (parameter.required) {
14471447
member.attributes.set('typeAttributes', ['required']);
1448+
} else {
1449+
member.attributes.set('typeAttributes', ['optional']);
14481450
}
14491451

14501452
return member;

packages/fury-adapter-swagger/test/fixtures/parameter-array-default-warning.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
"content": "Query argument"
5353
}
5454
},
55+
"attributes": {
56+
"typeAttributes": {
57+
"element": "array",
58+
"content": [
59+
{
60+
"element": "string",
61+
"content": "optional"
62+
}
63+
]
64+
}
65+
},
5566
"content": {
5667
"key": {
5768
"element": "string",

packages/fury-adapter-swagger/test/parameter-test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { Fury } = require('fury');
33
const Parser = require('../lib/parser');
44

55
const { minim: namespace } = new Fury();
6-
const { Annotation } = namespace.elements;
6+
const { Annotation, Member } = namespace.elements;
77

88
describe('Parameter to Member converter', () => {
99
it('can convert a parameter to a member with x-example', () => {
@@ -228,4 +228,34 @@ describe('Parameter to Member converter', () => {
228228
expect(parser.result.get(0)).to.be.instanceof(Annotation);
229229
expect(parameter.toValue()).to.equal('true');
230230
});
231+
232+
describe('#typeAttributes', () => {
233+
it('adds required typeAttribute for required as truthy', () => {
234+
const parser = new Parser({ namespace, source: '' });
235+
parser.result = new namespace.elements.ParseResult();
236+
const parameter = parser.convertParameterToMember({
237+
type: 'string',
238+
required: true,
239+
});
240+
241+
expect(parameter).to.be.instanceof(Member);
242+
243+
const typeAttributes = parameter.attributes.getValue('typeAttributes');
244+
expect(typeAttributes).to.deep.equal(['required']);
245+
});
246+
247+
it('adds required typeAttribute for required as truthy', () => {
248+
const parser = new Parser({ namespace, source: '' });
249+
parser.result = new namespace.elements.ParseResult();
250+
const parameter = parser.convertParameterToMember({
251+
type: 'string',
252+
required: false,
253+
});
254+
255+
expect(parameter).to.be.instanceof(Member);
256+
257+
const typeAttributes = parameter.attributes.getValue('typeAttributes');
258+
expect(typeAttributes).to.deep.equal(['optional']);
259+
});
260+
});
231261
});

packages/swagger-zoo/fixtures/features/api-elements/param-with-enum-default.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
"content": "Query argument"
5353
}
5454
},
55+
"attributes": {
56+
"typeAttributes": {
57+
"element": "array",
58+
"content": [
59+
{
60+
"element": "string",
61+
"content": "optional"
62+
}
63+
]
64+
}
65+
},
5566
"content": {
5667
"key": {
5768
"element": "string",
@@ -125,6 +136,17 @@
125136
"content": "Query argument 2"
126137
}
127138
},
139+
"attributes": {
140+
"typeAttributes": {
141+
"element": "array",
142+
"content": [
143+
{
144+
"element": "string",
145+
"content": "optional"
146+
}
147+
]
148+
}
149+
},
128150
"content": {
129151
"key": {
130152
"element": "string",

packages/swagger-zoo/fixtures/features/api-elements/param-with-enum-default.sourcemap.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@
175175
]
176176
}
177177
]
178+
},
179+
"typeAttributes": {
180+
"element": "array",
181+
"content": [
182+
{
183+
"element": "string",
184+
"content": "optional"
185+
}
186+
]
178187
}
179188
},
180189
"content": {
@@ -390,6 +399,15 @@
390399
]
391400
}
392401
]
402+
},
403+
"typeAttributes": {
404+
"element": "array",
405+
"content": [
406+
{
407+
"element": "string",
408+
"content": "optional"
409+
}
410+
]
393411
}
394412
},
395413
"content": {

packages/swagger-zoo/fixtures/features/api-elements/recursion.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@
113113
"content": "Test 1 2"
114114
}
115115
},
116+
"attributes": {
117+
"typeAttributes": {
118+
"element": "array",
119+
"content": [
120+
{
121+
"element": "string",
122+
"content": "optional"
123+
}
124+
]
125+
}
126+
},
116127
"content": {
117128
"key": {
118129
"element": "string",

packages/swagger-zoo/fixtures/features/api-elements/recursion.sourcemap.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@
311311
]
312312
}
313313
]
314+
},
315+
"typeAttributes": {
316+
"element": "array",
317+
"content": [
318+
{
319+
"element": "string",
320+
"content": "optional"
321+
}
322+
]
314323
}
315324
},
316325
"content": {

packages/swagger-zoo/fixtures/features/api-elements/x-example.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@
5757
},
5858
{
5959
"element": "member",
60+
"attributes": {
61+
"typeAttributes": {
62+
"element": "array",
63+
"content": [
64+
{
65+
"element": "string",
66+
"content": "optional"
67+
}
68+
]
69+
}
70+
},
6071
"content": {
6172
"key": {
6273
"element": "string",

packages/swagger-zoo/fixtures/features/api-elements/x-example.sourcemap.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@
178178
]
179179
}
180180
]
181+
},
182+
"typeAttributes": {
183+
"element": "array",
184+
"content": [
185+
{
186+
"element": "string",
187+
"content": "optional"
188+
}
189+
]
181190
}
182191
},
183192
"content": {

0 commit comments

Comments
 (0)