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

Commit 5a57139

Browse files
committed
perf(oas2): don't generate bodies when validating
1 parent 3a1727d commit 5a57139

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

packages/openapi2-parser/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Master
44

5+
### Enhancements
6+
7+
- Performance improvements to OpenAPI 2 validation, this will only apply to
8+
validation only. Full OpenAPI 2 parsing is not altered.
9+
510
### Bug Fixes
611

712
- Returns a validation error while trying to parse a YAML or JSON object which

packages/openapi2-parser/lib/adapter.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,29 @@ function parse(options) {
2929
});
3030
}
3131

32+
function validate(options) {
33+
return new Promise((fulfil, reject) => {
34+
const parser = new Parser({
35+
...options,
36+
generateMessageBody: false,
37+
generateMessageBodySchema: false,
38+
});
39+
40+
parser.parse((error, parseResult) => {
41+
if (error) {
42+
reject(error);
43+
} else if (parseResult.annotations.length > 0) {
44+
fulfil(new options.namespace.elements.ParseResult(parseResult.annotations));
45+
} else {
46+
fulfil(null);
47+
}
48+
});
49+
});
50+
}
51+
3252
/**
3353
* @implements {FuryAdapter}
3454
*/
3555
module.exports = {
36-
name, mediaTypes, detect, parse,
56+
name, mediaTypes, detect, validate, parse,
3757
};

packages/openapi2-parser/test/adapter-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,36 @@ describe('Swagger 2.0 adapter', () => {
104104
});
105105
});
106106

107+
context('#validate', () => {
108+
it('validates source with no annotations', (done) => {
109+
const source = { swagger: '2.0', info: { title: 'Test', version: '1.0' } };
110+
111+
fury.validate({ source }, (err, parseResult) => {
112+
expect(err).to.be.null;
113+
expect(parseResult).to.be.null;
114+
done();
115+
});
116+
});
117+
118+
it('validates source with warnings', (done) => {
119+
const source = {
120+
swagger: '2.0',
121+
};
122+
123+
fury.validate({ source }, (err, parseResult) => {
124+
expect(err).to.be.null;
125+
expect(parseResult).to.be.instanceof(fury.minim.elements.ParseResult);
126+
expect(parseResult.length).to.equal(3);
127+
expect(parseResult.toValue()).to.deep.equal([
128+
'Missing required property: title',
129+
'Source maps are only available with string input',
130+
'Missing required property: version',
131+
]);
132+
done();
133+
});
134+
});
135+
});
136+
107137
context('can parse Swagger object', () => {
108138
const source = { swagger: '2.0', info: { title: 'Test', version: '1.0' } };
109139
let result;

0 commit comments

Comments
 (0)