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

Commit 4139afa

Browse files
committed
feat(oas3): support generating message body from schema
1 parent e804698 commit 4139afa

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
- Added primitive support for 'examples' in 'Media Type Object'. The first
88
example value is used for JSON media types.
99

10+
- Added primitive support for generating a JSON message body from a schema for
11+
JSON media types. Referencing is not supported for this feature.
12+
1013
## 0.6.0 (2019-02-26)
1114

1215
### Enhancements

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ function parseMediaTypeObject(context, MessageBodyClass, element) {
113113
}
114114

115115
const dataStructure = mediaTypeObject.get('schema');
116+
117+
if (!messageBody && dataStructure && isJSONMediaType(mediaType)) {
118+
const value = dataStructure.content.valueOf();
119+
120+
if (value) {
121+
const body = JSON.stringify(value);
122+
const asset = new namespace.elements.Asset(body);
123+
asset.classes.push('messageBody');
124+
asset.contentType = mediaType;
125+
message.push(asset);
126+
}
127+
}
128+
116129
if (dataStructure) {
117130
message.push(dataStructure);
118131
}

packages/fury-adapter-oas3-parser/test/integration/fixtures/components/operation-object-requestBody.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@
142142
}
143143
},
144144
"content": [
145+
{
146+
"element": "asset",
147+
"meta": {
148+
"classes": {
149+
"element": "array",
150+
"content": [
151+
{
152+
"element": "string",
153+
"content": "messageBody"
154+
}
155+
]
156+
}
157+
},
158+
"attributes": {
159+
"contentType": {
160+
"element": "string",
161+
"content": "application/json"
162+
}
163+
},
164+
"content": "{}"
165+
},
145166
{
146167
"element": "dataStructure",
147168
"content": {

packages/fury-adapter-oas3-parser/test/integration/fixtures/components/path-item-object-parameters-unsupported-parameter.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@
8282
}
8383
},
8484
"content": [
85+
{
86+
"element": "asset",
87+
"meta": {
88+
"classes": {
89+
"element": "array",
90+
"content": [
91+
{
92+
"element": "string",
93+
"content": "messageBody"
94+
}
95+
]
96+
}
97+
},
98+
"attributes": {
99+
"contentType": {
100+
"element": "string",
101+
"content": "application/json"
102+
}
103+
},
104+
"content": "[]"
105+
},
85106
{
86107
"element": "dataStructure",
87108
"content": {

packages/fury-adapter-oas3-parser/test/integration/fixtures/components/path-item-object-parameters.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,27 @@
139139
}
140140
},
141141
"content": [
142+
{
143+
"element": "asset",
144+
"meta": {
145+
"classes": {
146+
"element": "array",
147+
"content": [
148+
{
149+
"element": "string",
150+
"content": "messageBody"
151+
}
152+
]
153+
}
154+
},
155+
"attributes": {
156+
"contentType": {
157+
"element": "string",
158+
"content": "application/json"
159+
}
160+
},
161+
"content": "[]"
162+
},
142163
{
143164
"element": "dataStructure",
144165
"content": {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,26 @@ describe('Media Type Object', () => {
239239
expect(message.dataStructure).to.be.instanceof(namespace.elements.DataStructure);
240240
expect(message.dataStructure.content.element).to.equal('User');
241241
});
242+
243+
it('generates an messageBody asset for JSON type with no examples', () => {
244+
const mediaType = new namespace.elements.Member('application/json', {
245+
schema: {
246+
type: 'object',
247+
properties: {
248+
name: {
249+
type: 'string',
250+
example: 'doe',
251+
},
252+
},
253+
},
254+
});
255+
256+
const parseResult = parse(context, messageBodyClass, mediaType);
257+
258+
const message = parseResult.get(0);
259+
expect(message).to.be.instanceof(messageBodyClass);
260+
expect(message.messageBody.toValue()).to.equal('{"name":"doe"}');
261+
expect(message.messageBody.contentType.toValue()).to.equal('application/json');
262+
});
242263
});
243264
});

0 commit comments

Comments
 (0)