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

Commit 9b41de9

Browse files
committed
fix(oas3): disallow non-string text examples
1 parent 148f496 commit 9b41de9

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

packages/openapi3-parser/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# API Elements: OpenAPI 3 Parser Changelog
22

3+
## Master
4+
5+
### Bug Fixes
6+
7+
- Return a warning when parsing a document with a 'Media Type Object'
8+
using a text based media type when the example value is not a string.
9+
Previously an invalid asset element was created which contained non-string
10+
content.
11+
312
## 0.14.2 (2020-07-20)
413

514
### Bug Fixes

packages/openapi3-parser/lib/parser/oas/parseMediaTypeObject.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const contentTyper = require('content-type');
33
const mediaTyper = require('media-typer');
44
const pipeParseResult = require('../../pipeParseResult');
55
const {
6-
isExtension, hasKey, getKey, getValue,
6+
isString, isExtension, hasKey, getKey, getValue,
77
} = require('../../predicates');
88
const {
99
createWarning,
@@ -89,19 +89,28 @@ const createTextMessageBodyAsset = R.curry((namespace, mediaType, value) => {
8989
return asset;
9090
});
9191

92-
function parseExample(namespace, mediaType) {
93-
const createExampleNotJSONWarning = createWarning(namespace,
92+
function parseExample(context, mediaType) {
93+
const { namespace } = context;
94+
95+
const createExampleNotSupportedForMediaTypeWarning = createWarning(namespace,
9496
`'${name}' 'example' is not supported for media type '${mediaType}'`);
9597

98+
const createExampleNotStringWarning = createWarning(namespace,
99+
`'${name}' 'example' should be a string for media type '${mediaType}'`);
100+
96101
const isTextBasedType = R.anyPass([
97102
() => isTextMediaType(mediaType),
98103
() => isXMLMediaType(mediaType),
99104
]);
100105

106+
const parseTextExample = pipeParseResult(namespace,
107+
R.unless(isString, createExampleNotStringWarning),
108+
createTextMessageBodyAsset(namespace, mediaType));
109+
101110
return R.compose(R.cond([
102111
[() => isJSONMediaType(mediaType), createJSONMessageBodyAsset(namespace, mediaType)],
103-
[() => isTextBasedType(mediaType), createTextMessageBodyAsset(namespace, mediaType)],
104-
[R.T, createExampleNotJSONWarning],
112+
[() => isTextBasedType(mediaType), parseTextExample],
113+
[R.T, createExampleNotSupportedForMediaTypeWarning],
105114
]), getValue);
106115
}
107116

@@ -167,7 +176,7 @@ function parseMediaTypeObject(context, MessageBodyClass, element) {
167176
createJSONMessageBodyAsset(namespace, mediaType));
168177

169178
const parseMember = R.cond([
170-
[hasKey('example'), parseExample(namespace, mediaType)],
179+
[hasKey('example'), parseExample(context, mediaType)],
171180
[hasKey('examples'), R.compose(parseExamples, getValue)],
172181
[hasKey('schema'), R.compose(parseSchemaObjectOrRef(context), getValue)],
173182

packages/openapi3-parser/test/unit/parser/oas/parseMediaTypeObject-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ describe('Media Type Object', () => {
143143
"'Media Type Object' 'example' is not supported for media type 'application/plist'"
144144
);
145145
});
146+
147+
it('warns for non-string example with text type', () => {
148+
const mediaType = new namespace.elements.Member('text/plain', {
149+
example: { message: 'Hello World' },
150+
});
151+
152+
const parseResult = parse(context, messageBodyClass, mediaType);
153+
154+
expect(parseResult).to.contain.warning(
155+
"'Media Type Object' 'example' should be a string for media type 'text/plain'"
156+
);
157+
158+
const message = parseResult.get(0);
159+
expect(message).to.be.instanceof(messageBodyClass);
160+
expect(message.messageBody).to.be.undefined;
161+
});
146162
});
147163

148164
describe('#examples', () => {

0 commit comments

Comments
 (0)