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

Commit 7619031

Browse files
committed
feat(oas2): add support for generateMessageBodySchema
1 parent 5b81fab commit 7619031

File tree

5 files changed

+584
-22
lines changed

5 files changed

+584
-22
lines changed

packages/fury-adapter-swagger/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
### Enhancements
66

77
- The parser can now be configured to disable generation of example message
8-
bodies by providing an adapter option `generateMessageBody` as `false` during
9-
parse.
8+
bodies and message body schemas by providing an adapter option
9+
`generateMessageBody` or `generateMessageBodySchema` as `false` during parse.
1010

1111
## 0.28.3 (2020-03-16)
1212

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const {
2424
// interface.
2525
class Parser {
2626
constructor({
27-
namespace, source, generateSourceMap, generateMessageBody,
27+
namespace, source, generateSourceMap, generateMessageBody, generateMessageBodySchema,
2828
}) {
2929
// Parser options
3030
this.namespace = namespace;
@@ -37,6 +37,12 @@ class Parser {
3737
this.generateMessageBody = generateMessageBody;
3838
}
3939

40+
if (generateMessageBodySchema === undefined) {
41+
this.generateMessageBodySchema = true;
42+
} else {
43+
this.generateMessageBodySchema = generateMessageBodySchema;
44+
}
45+
4046
// Global scheme requirements
4147
this.globalSchemes = [];
4248

@@ -1598,8 +1604,6 @@ class Parser {
15981604
}
15991605

16001606
pushAssets(schema, payload, contentType, pushBody) {
1601-
let jsonSchema;
1602-
16031607
if (this.bodyCache === undefined) {
16041608
this.bodyCache = {};
16051609
}
@@ -1610,28 +1614,34 @@ class Parser {
16101614
cacheKey = `${referencedPathValue.$ref};${contentType}`;
16111615
}
16121616

1613-
try {
1614-
const root = { definitions: this.definitions };
1615-
jsonSchema = convertSchema(referencedPathValue || schema, root,
1616-
this.referencedSwagger);
1617-
} catch (error) {
1618-
this.createAnnotation(annotations.VALIDATION_ERROR, this.path, error.message);
1619-
return;
1620-
}
1617+
if (this.generateMessageBody || this.generateMessageBodySchema) {
1618+
let jsonSchema;
1619+
try {
1620+
const root = { definitions: this.definitions };
1621+
jsonSchema = convertSchema(referencedPathValue || schema, root,
1622+
this.referencedSwagger);
1623+
} catch (error) {
1624+
this.createAnnotation(annotations.VALIDATION_ERROR, this.path, error.message);
1625+
return;
1626+
}
16211627

1622-
if (pushBody && this.generateMessageBody) {
1623-
if (cacheKey && this.bodyCache[cacheKey]) {
1624-
const asset = this.bodyCache[cacheKey];
1625-
payload.push(asset.clone());
1626-
} else {
1627-
const asset = bodyFromSchema(jsonSchema, payload, this, contentType);
1628-
if (cacheKey) {
1629-
this.bodyCache[cacheKey] = asset;
1628+
if (pushBody && this.generateMessageBody) {
1629+
if (cacheKey && this.bodyCache[cacheKey]) {
1630+
const asset = this.bodyCache[cacheKey];
1631+
payload.push(asset.clone());
1632+
} else {
1633+
const asset = bodyFromSchema(jsonSchema, payload, this, contentType);
1634+
if (cacheKey) {
1635+
this.bodyCache[cacheKey] = asset;
1636+
}
16301637
}
16311638
}
1639+
1640+
if (this.generateMessageBodySchema) {
1641+
this.pushSchemaAsset(schema, jsonSchema, payload, this.path);
1642+
}
16321643
}
16331644

1634-
this.pushSchemaAsset(schema, jsonSchema, payload, this.path);
16351645
this.pushDataStructureAsset(referencedPathValue || schema, payload);
16361646
}
16371647

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,22 @@ describe('Swagger 2.0 adapter', () => {
220220
path.join(__dirname, 'fixtures', 'options', 'generateMessageBody-false.json')
221221
), { generateMessageBody: false });
222222
});
223+
224+
describe('#generateMessageBodySchema', () => {
225+
testFixture('generates message body schema by default', testFixtureOptions(
226+
path.join(__dirname, 'fixtures', 'json-body-generation.yaml'),
227+
path.join(__dirname, 'fixtures', 'options', 'generateMessageBodySchema-true.json')
228+
));
229+
230+
testFixture('generates message body schema when generateMessageBodySchema is true', testFixtureOptions(
231+
path.join(__dirname, 'fixtures', 'json-body-generation.yaml'),
232+
path.join(__dirname, 'fixtures', 'options', 'generateMessageBodySchema-true.json')
233+
), { generateMessageBodySchema: true });
234+
235+
testFixture('disables generating message body schema when requested', testFixtureOptions(
236+
path.join(__dirname, 'fixtures', 'json-body-generation.yaml'),
237+
path.join(__dirname, 'fixtures', 'options', 'generateMessageBodySchema-false.json')
238+
), { generateMessageBodySchema: false });
239+
});
223240
});
224241
});
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
{
2+
"element": "parseResult",
3+
"content": [
4+
{
5+
"element": "category",
6+
"meta": {
7+
"classes": {
8+
"element": "array",
9+
"content": [
10+
{
11+
"element": "string",
12+
"content": "api"
13+
}
14+
]
15+
},
16+
"title": {
17+
"element": "string",
18+
"content": "Data Structure Generation"
19+
}
20+
},
21+
"attributes": {
22+
"version": {
23+
"element": "string",
24+
"content": "1.0.0"
25+
}
26+
},
27+
"content": [
28+
{
29+
"element": "resource",
30+
"attributes": {
31+
"href": {
32+
"element": "string",
33+
"content": "/user"
34+
}
35+
},
36+
"content": [
37+
{
38+
"element": "transition",
39+
"meta": {
40+
"id": {
41+
"element": "string",
42+
"content": "getResource"
43+
}
44+
},
45+
"content": [
46+
{
47+
"element": "copy",
48+
"content": "Get a resource"
49+
},
50+
{
51+
"element": "httpTransaction",
52+
"content": [
53+
{
54+
"element": "httpRequest",
55+
"attributes": {
56+
"method": {
57+
"element": "string",
58+
"content": "GET"
59+
},
60+
"headers": {
61+
"element": "httpHeaders",
62+
"content": [
63+
{
64+
"element": "member",
65+
"meta": {
66+
"links": {
67+
"element": "array",
68+
"content": [
69+
{
70+
"element": "link",
71+
"attributes": {
72+
"relation": {
73+
"element": "string",
74+
"content": "inferred"
75+
},
76+
"href": {
77+
"element": "string",
78+
"content": "http://docs.apiary.io/validations/swagger#produces-accept"
79+
}
80+
}
81+
}
82+
]
83+
}
84+
},
85+
"content": {
86+
"key": {
87+
"element": "string",
88+
"content": "Accept"
89+
},
90+
"value": {
91+
"element": "string",
92+
"content": "application/json"
93+
}
94+
}
95+
}
96+
]
97+
}
98+
}
99+
},
100+
{
101+
"element": "httpResponse",
102+
"attributes": {
103+
"headers": {
104+
"element": "httpHeaders",
105+
"content": [
106+
{
107+
"element": "member",
108+
"meta": {
109+
"links": {
110+
"element": "array",
111+
"content": [
112+
{
113+
"element": "link",
114+
"attributes": {
115+
"relation": {
116+
"element": "string",
117+
"content": "inferred"
118+
},
119+
"href": {
120+
"element": "string",
121+
"content": "http://docs.apiary.io/validations/swagger#produces-content-type"
122+
}
123+
}
124+
}
125+
]
126+
}
127+
},
128+
"content": {
129+
"key": {
130+
"element": "string",
131+
"content": "Content-Type"
132+
},
133+
"value": {
134+
"element": "string",
135+
"content": "application/json"
136+
}
137+
}
138+
}
139+
]
140+
},
141+
"statusCode": {
142+
"element": "string",
143+
"content": "200"
144+
}
145+
},
146+
"content": [
147+
{
148+
"element": "copy",
149+
"content": "response description"
150+
},
151+
{
152+
"element": "asset",
153+
"meta": {
154+
"classes": {
155+
"element": "array",
156+
"content": [
157+
{
158+
"element": "string",
159+
"content": "messageBody"
160+
}
161+
]
162+
},
163+
"links": {
164+
"element": "array",
165+
"content": [
166+
{
167+
"element": "link",
168+
"attributes": {
169+
"relation": {
170+
"element": "string",
171+
"content": "inferred"
172+
},
173+
"href": {
174+
"element": "string",
175+
"content": "http://docs.apiary.io/validations/swagger#message-body-generation"
176+
}
177+
}
178+
}
179+
]
180+
}
181+
},
182+
"attributes": {
183+
"contentType": {
184+
"element": "string",
185+
"content": "application/json"
186+
}
187+
},
188+
"content": "{\n \"id\": 1,\n \"name\": \"doe\"\n}"
189+
},
190+
{
191+
"element": "dataStructure",
192+
"content": {
193+
"element": "object",
194+
"content": [
195+
{
196+
"element": "member",
197+
"attributes": {
198+
"typeAttributes": {
199+
"element": "array",
200+
"content": [
201+
{
202+
"element": "string",
203+
"content": "optional"
204+
}
205+
]
206+
}
207+
},
208+
"content": {
209+
"key": {
210+
"element": "string",
211+
"content": "id"
212+
},
213+
"value": {
214+
"element": "number",
215+
"content": 1
216+
}
217+
}
218+
},
219+
{
220+
"element": "member",
221+
"attributes": {
222+
"typeAttributes": {
223+
"element": "array",
224+
"content": [
225+
{
226+
"element": "string",
227+
"content": "optional"
228+
}
229+
]
230+
}
231+
},
232+
"content": {
233+
"key": {
234+
"element": "string",
235+
"content": "name"
236+
},
237+
"value": {
238+
"element": "string",
239+
"content": "doe"
240+
}
241+
}
242+
}
243+
]
244+
}
245+
}
246+
]
247+
}
248+
]
249+
}
250+
]
251+
}
252+
]
253+
}
254+
]
255+
}
256+
]
257+
}

0 commit comments

Comments
 (0)