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

Commit a69180d

Browse files
authored
Support text-based examples in OAS 3 (#321)
Support text-based examples in OAS 3
2 parents 0288e87 + f3f4ee2 commit a69180d

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
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+
### Enhancements
6+
7+
- Adds support for example values for text based media types such as
8+
`text/*` and `application/xml`.
9+
310
## 0.9.0 (2019-07-02)
411

512
This release brings significant performance improvement to parsing large

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ function isTextMediaType(mediaType) {
3131
return type === 'text';
3232
}
3333

34+
function isXMLMediaType(mediaType) {
35+
const contentType = contentTyper.parse(mediaType);
36+
const { type, suffix, subtype } = mediaTyper.parse(contentType.type);
37+
return type === 'application' && (suffix === 'xml' || subtype === 'xml');
38+
}
39+
3440
const canGenerateMessageBodyForMediaType = R.anyPass([isJSONMediaType, isTextMediaType]);
3541

3642
function generateMessageBody(context, mediaType, dataStructure) {
@@ -76,13 +82,27 @@ const createJSONMessageBodyAsset = R.curry((namespace, mediaType, value) => {
7682
return asset;
7783
});
7884

85+
const createTextMessageBodyAsset = R.curry((namespace, mediaType, value) => {
86+
const asset = new namespace.elements.Asset(value.toValue());
87+
asset.classes.push('messageBody');
88+
asset.contentType = mediaType;
89+
return asset;
90+
});
91+
7992
function parseExample(namespace, mediaType) {
8093
const createExampleNotJSONWarning = createWarning(namespace,
81-
"'Media Type Object' 'example' is only supported for JSON media types");
94+
`'${name}' 'example' is not supported for media type '${mediaType}'`);
95+
96+
const isTextBasedType = R.anyPass([
97+
() => isTextMediaType(mediaType),
98+
() => isXMLMediaType(mediaType),
99+
]);
82100

83-
return R.ifElse(() => isJSONMediaType(mediaType),
84-
R.compose(createJSONMessageBodyAsset(namespace, mediaType), getValue),
85-
createExampleNotJSONWarning);
101+
return R.compose(R.cond([
102+
[() => isJSONMediaType(mediaType), createJSONMessageBodyAsset(namespace, mediaType)],
103+
[() => isTextBasedType(mediaType), createTextMessageBodyAsset(namespace, mediaType)],
104+
[R.T, createExampleNotJSONWarning],
105+
]), getValue);
86106
}
87107

88108
const parseSchemaObjectOrRef = parseReference('schemas', parseSchemaObject);

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,34 @@ describe('Media Type Object', () => {
9292
expect(message.messageBody.contentType.toValue()).to.equal('application/hal+json');
9393
});
9494

95-
it('warns for example without JSON type', () => {
95+
it('creates an messageBody asset for text type with text example', () => {
96+
const mediaType = new namespace.elements.Member('text/plain', {
97+
example: 'Hello World',
98+
});
99+
100+
const parseResult = parse(context, messageBodyClass, mediaType);
101+
102+
const message = parseResult.get(0);
103+
expect(message).to.be.instanceof(messageBodyClass);
104+
expect(message.messageBody.toValue()).to.equal('Hello World');
105+
expect(message.messageBody.contentType.toValue()).to.equal('text/plain');
106+
});
107+
108+
it('creates an messageBody asset for text type with xml example', () => {
96109
const mediaType = new namespace.elements.Member('application/xml', {
110+
example: '<?xml version="1.0" encoding="UTF-8"?>',
111+
});
112+
113+
const parseResult = parse(context, messageBodyClass, mediaType);
114+
115+
const message = parseResult.get(0);
116+
expect(message).to.be.instanceof(messageBodyClass);
117+
expect(message.messageBody.toValue()).to.equal('<?xml version="1.0" encoding="UTF-8"?>');
118+
expect(message.messageBody.contentType.toValue()).to.equal('application/xml');
119+
});
120+
121+
it('warns for example without supported media type', () => {
122+
const mediaType = new namespace.elements.Member('application/plist', {
97123
example: {
98124
message: 'Hello World',
99125
},
@@ -106,7 +132,7 @@ describe('Media Type Object', () => {
106132
expect(message.messageBody).to.be.undefined;
107133

108134
expect(parseResult).to.contain.warning(
109-
"'Media Type Object' 'example' is only supported for JSON media types"
135+
"'Media Type Object' 'example' is not supported for media type 'application/plist'"
110136
);
111137
});
112138
});

0 commit comments

Comments
 (0)