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

Commit c01a2e8

Browse files
committed
fix(oas3): validate media types
1 parent 853dfac commit c01a2e8

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

packages/fury-adapter-oas3-parser/CHANGELOG.md

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

3+
## Master
4+
5+
### Bug Fixes
6+
7+
- Added validation of media types, previously we would throw an error while
8+
handling invalid media types.
9+
310
## 0.7.0 (2019-03-26)
411

512
### Enhancements

packages/fury-adapter-oas3-parser/lib/parser/oas/parseMediaTypeObject.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const R = require('ramda');
22
const mediaTyper = require('media-typer');
33
const pipeParseResult = require('../../pipeParseResult');
4-
const { isExtension, hasKey, getValue } = require('../../predicates');
4+
const {
5+
isExtension, hasKey, getKey, getValue,
6+
} = require('../../predicates');
57
const {
68
createWarning,
79
createUnsupportedMemberWarning,
@@ -44,6 +46,15 @@ function parseExample(namespace, mediaType) {
4446
const parseSchemaObjectOrRef = parseReference('schemas', parseSchemaObject);
4547
const parseExampleObjectOrRef = parseReference('examples', parseExampleObject);
4648

49+
const isValidMediaType = (mediaType) => {
50+
try {
51+
mediaTyper.parse(mediaType.toValue());
52+
} catch (error) {
53+
return false;
54+
}
55+
return true;
56+
};
57+
4758
/**
4859
* Parse Media Type Object
4960
*
@@ -59,6 +70,14 @@ function parseMediaTypeObject(context, MessageBodyClass, element) {
5970
const { namespace } = context;
6071
const mediaType = element.key.toValue();
6172

73+
const createInvalidMediaTypeWarning = mediaType => createWarning(namespace,
74+
`'${name}' media type '${mediaType.toValue()}' is invalid`, mediaType);
75+
76+
const validateMediaType = R.unless(
77+
R.compose(isValidMediaType, getKey),
78+
R.compose(createInvalidMediaTypeWarning, getKey)
79+
);
80+
6281
const createExamplesNotJSONWarning = createWarning(namespace,
6382
`'${name}' 'examples' is only supported for JSON media types`);
6483

@@ -99,6 +118,8 @@ function parseMediaTypeObject(context, MessageBodyClass, element) {
99118
]);
100119

101120
const parseMediaType = pipeParseResult(namespace,
121+
validateMediaType,
122+
getValue,
102123
parseObject(context, name, parseMember),
103124
(mediaTypeObject) => {
104125
const message = new MessageBodyClass();
@@ -146,7 +167,7 @@ function parseMediaTypeObject(context, MessageBodyClass, element) {
146167
return message;
147168
});
148169

149-
return parseMediaType(element.value);
170+
return parseMediaType(element);
150171
}
151172

152173
module.exports = R.curry(parseMediaTypeObject);

packages/fury-adapter-oas3-parser/test/unit/parser/oas/parseMediaTypeObject-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ describe('Media Type Object', () => {
2121
expect(parseResult).to.contain.warning("'Media Type Object' is not an object");
2222
});
2323

24+
it('provides warning when media type is invalid', () => {
25+
const mediaType = new namespace.elements.Member('foo', {});
26+
27+
const parseResult = parse(context, messageBodyClass, mediaType);
28+
29+
expect(parseResult).to.contain.warning("'Media Type Object' media type 'foo' is invalid");
30+
});
31+
2432
it('returns a HTTP message body', () => {
2533
const mediaType = new namespace.elements.Member('application/json', {});
2634

0 commit comments

Comments
 (0)