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

Commit 91d1b8e

Browse files
committed
feat(oas2): add support for generateMessageBody
1 parent 1e32340 commit 91d1b8e

File tree

5 files changed

+586
-13
lines changed

5 files changed

+586
-13
lines changed

packages/fury-adapter-swagger/CHANGELOG.md

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

3+
## Master
4+
5+
### Enhancements
6+
7+
- 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.
10+
311
## 0.28.3 (2020-03-16)
412

513
### Enhancements

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ const {
2323
// the input Swagger into Refract elements. The `parse` function is its main
2424
// interface.
2525
class Parser {
26-
constructor({ namespace, source, generateSourceMap }) {
26+
constructor({
27+
namespace, source, generateSourceMap, generateMessageBody,
28+
}) {
2729
// Parser options
2830
this.namespace = namespace;
2931
this.source = source;
3032
this.generateSourceMap = generateSourceMap;
3133

34+
if (generateMessageBody === undefined) {
35+
this.generateMessageBody = true;
36+
} else {
37+
this.generateMessageBody = generateMessageBody;
38+
}
39+
3240
// Global scheme requirements
3341
this.globalSchemes = [];
3442

@@ -1110,9 +1118,11 @@ class Parser {
11101118
pushHeader('Content-Type', FORM_CONTENT_TYPE, request, this, 'form-data-content-type');
11111119
}
11121120

1113-
const jsonSchema = convertSchema(schema, { definitions: this.definitions },
1114-
this.referencedSwagger);
1115-
bodyFromSchema(jsonSchema, request, this, contentType || FORM_CONTENT_TYPE);
1121+
if (this.generateMessageBody) {
1122+
const jsonSchema = convertSchema(schema, { definitions: this.definitions },
1123+
this.referencedSwagger);
1124+
bodyFromSchema(jsonSchema, request, this, contentType || FORM_CONTENT_TYPE);
1125+
}
11161126

11171127
// Generating data structure
11181128
const dataStructure = new DataStructure();
@@ -1609,7 +1619,7 @@ class Parser {
16091619
return;
16101620
}
16111621

1612-
if (pushBody) {
1622+
if (pushBody && this.generateMessageBody) {
16131623
if (cacheKey && this.bodyCache[cacheKey]) {
16141624
const asset = this.bodyCache[cacheKey];
16151625
payload.push(asset.clone());

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ const { detect } = adapter;
1616

1717
fury.adapters = [adapter];
1818

19-
function testFixture(description, fixture, generateSourceMap = false) {
19+
function testFixture(description, fixture, adapterOptions) {
2020
it(description, (done) => {
2121
const source = fixture.swagger;
2222
let expected;
2323

24-
if (generateSourceMap) {
24+
if (adapterOptions && adapterOptions.generateSourceMap) {
2525
expected = fixture.apiElementsSourceMap;
2626
} else {
2727
expected = fixture.apiElements;
2828
}
2929

3030
const mediaType = 'application/swagger+yaml';
3131

32-
fury.parse({ source, mediaType, generateSourceMap }, (err, output) => {
32+
fury.parse({ source, mediaType, adapterOptions }, (err, output) => {
3333
if (err && !output) {
3434
return done(err);
3535
}
@@ -40,7 +40,7 @@ function testFixture(description, fixture, generateSourceMap = false) {
4040
if (process.env.GENERATE) {
4141
expected = fury.minim.toRefract(output);
4242

43-
if (generateSourceMap) {
43+
if (adapterOptions && adapterOptions.generateSourceMap) {
4444
// eslint-disable-next-line no-param-reassign
4545
fixture.apiElementsSourceMap = expected;
4646
} else {
@@ -55,6 +55,24 @@ function testFixture(description, fixture, generateSourceMap = false) {
5555
});
5656
}
5757

58+
function testFixtureOptions(swaggerPath, apiElementsPath) {
59+
const swagger = fs.readFileSync(swaggerPath, 'utf-8');
60+
const options = { swagger };
61+
62+
Object.defineProperty(options, 'apiElements', {
63+
get() {
64+
return require(apiElementsPath);
65+
},
66+
67+
set(value) {
68+
fs.writeFileSync(apiElementsPath, JSON.stringify(value, null, 2));
69+
return value;
70+
},
71+
});
72+
73+
return options;
74+
}
75+
5876
describe('Swagger 2.0 adapter', () => {
5977
context('detection', () => {
6078
it('detects JSON', () => {
@@ -141,17 +159,17 @@ describe('Swagger 2.0 adapter', () => {
141159
const fixtures = swaggerZoo.features();
142160
fixtures.forEach((fixture) => {
143161
testFixture(`Parses ${fixture.name}`, fixture);
144-
testFixture(`Parses ${fixture.name} with source maps`, fixture, true);
162+
testFixture(`Parses ${fixture.name} with source maps`, fixture, { generateSourceMap: true });
145163
});
146164
});
147165

148166
describe('can parse regression fixtures', () => {
149167
const files = glob.sync(path.join(__dirname, 'fixtures', '*.yaml'));
150168

151-
files.forEach((file) => {
152-
const name = path.basename(file, path.extname(file));
169+
files.forEach((swaggerPath) => {
170+
const name = path.basename(swaggerPath, path.extname(swaggerPath));
153171

154-
const swagger = fs.readFileSync(file, 'utf-8');
172+
const swagger = fs.readFileSync(swaggerPath, 'utf-8');
155173
const apiElementsPath = path.join(__dirname, 'fixtures', `${name}.json`);
156174
const apiElementsSourceMapPath = path.join(__dirname, 'fixtures', `${name}.sourcemap.json`);
157175

@@ -181,6 +199,26 @@ describe('Swagger 2.0 adapter', () => {
181199

182200
testFixture(`Parses ${name}`, options);
183201
testFixture(`Parses ${name} with source maps`, options, true);
202+
testFixture(`Parses ${name}`, testFixtureOptions(swaggerPath, apiElementsPath));
203+
});
204+
});
205+
206+
describe('#adapterOptions', () => {
207+
describe('#generateMessageBody', () => {
208+
testFixture('generates message body by default', testFixtureOptions(
209+
path.join(__dirname, 'fixtures', 'json-body-generation.yaml'),
210+
path.join(__dirname, 'fixtures', 'options', 'generateMessageBody-true.json')
211+
));
212+
213+
testFixture('generates message body when generateMessageBody is true', testFixtureOptions(
214+
path.join(__dirname, 'fixtures', 'json-body-generation.yaml'),
215+
path.join(__dirname, 'fixtures', 'options', 'generateMessageBody-true.json')
216+
), { generateMessageBody: true });
217+
218+
testFixture('disables generating message body when requested', testFixtureOptions(
219+
path.join(__dirname, 'fixtures', 'json-body-generation.yaml'),
220+
path.join(__dirname, 'fixtures', 'options', 'generateMessageBody-false.json')
221+
), { generateMessageBody: false });
184222
});
185223
});
186224
});

0 commit comments

Comments
 (0)