Skip to content

Commit ff3d3a7

Browse files
authored
HCK-5261: add request body support for all types of request for OpenAPI 3.1 (#71)
1 parent b94a1a5 commit ff3d3a7

File tree

2 files changed

+110
-73
lines changed

2 files changed

+110
-73
lines changed

forward_engineering/helpers/componentsHelpers/requestBodiesHelper.js

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,67 @@ const get = require('lodash.get');
22
const { getContent } = require('./parametersHelper');
33
const getExtensions = require('../extensionsHelper');
44
const { getRef, hasRef } = require('../typeHelper');
5+
const { isTargetVersionJSONSchemaCompatible } = require('../sharedHelper');
56
const { commentDeactivatedItemInner } = require('../commentsHelper');
67
const { activateItem } = require('../commonHelper');
78

89
function getRequestBodies(data, specVersion) {
910
if (!data || !data.properties) {
1011
return;
11-
}
12-
13-
return Object.entries(data.properties)
14-
.map(([key, value]) => {
15-
return {
16-
key,
17-
value: mapRequestBody({ data: activateItem(value), required: get(data, 'required', []).includes(key), isParentActivated: true, specVersion }),
18-
};
19-
})
20-
.reduce((acc, { key, value }) => {
21-
acc[key] = value;
22-
return acc;
23-
}, {});
12+
}
13+
14+
return Object.entries(data.properties)
15+
.map(([key, value]) => {
16+
return {
17+
key,
18+
value: mapRequestBody({
19+
data: activateItem(value),
20+
required: get(data, 'required', []).includes(key),
21+
isParentActivated: true,
22+
specVersion,
23+
}),
24+
};
25+
})
26+
.reduce((acc, { key, value }) => {
27+
acc[key] = value;
28+
return acc;
29+
}, {});
2430
}
2531

2632
function mapRequestBody({ data, required, isParentActivated = false, specVersion }) {
27-
if (!data) {
28-
return;
29-
}
30-
if (hasRef(data)) {
33+
if (!data) {
34+
return;
35+
}
36+
if (hasRef(data)) {
3137
return commentDeactivatedItemInner(getRef(data, specVersion), data.isActivated, isParentActivated);
3238
}
3339

34-
const content = getContent({ data, isParentActivated: data.isActivated && isParentActivated, specVersion });
35-
if (!content) {
36-
return;
37-
}
38-
const description = data.description;
39-
const requestBody = {
40-
description,
41-
content,
42-
required: required || undefined
43-
}
44-
const extensions = getExtensions(data.scopesExtensions);
45-
46-
return commentDeactivatedItemInner(Object.assign({}, requestBody, extensions), data.isActivated, isParentActivated);
40+
const content = getContent({ data, isParentActivated: data.isActivated && isParentActivated, specVersion });
41+
if (!content) {
42+
return;
43+
}
44+
const description = data.description;
45+
const requestBody = {
46+
description,
47+
content,
48+
required: required || undefined,
49+
};
50+
const extensions = getExtensions(data.scopesExtensions);
51+
52+
return commentDeactivatedItemInner(Object.assign({}, requestBody, extensions), data.isActivated, isParentActivated);
53+
}
54+
55+
function getIsRequestBodySupported({ collectionName, specVersion }) {
56+
if (isTargetVersionJSONSchemaCompatible(specVersion)) {
57+
return true;
58+
}
59+
60+
const requestType = String(collectionName).toLowerCase();
61+
return requestType !== 'get' && requestType !== 'delete';
4762
}
4863

4964
module.exports = {
50-
getRequestBodies,
51-
mapRequestBody
52-
};
65+
getRequestBodies,
66+
mapRequestBody,
67+
getIsRequestBodySupported,
68+
};

forward_engineering/helpers/pathHelper.js

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const commonHelper = require('./commonHelper');
33
const getExtensions = require('./extensionsHelper');
44
const { getServers } = require('./serversHelper');
55
const { mapParameter } = require('./componentsHelpers/parametersHelper');
6-
const { mapRequestBody } = require('./componentsHelpers/requestBodiesHelper');
6+
const { mapRequestBody, getIsRequestBodySupported } = require('./componentsHelpers/requestBodiesHelper');
77
const { mapResponse } = require('./componentsHelpers/responsesHelper');
88
const { hasRef, getRef } = require('./typeHelper');
99
const { getArrayItems } = require('./sharedHelper');
@@ -13,16 +13,22 @@ function getPaths(containers, containersIdsForCallbacks = [], specVersion) {
1313
.filter(({ id }) => !containersIdsForCallbacks.includes(id))
1414
.reduce((acc, container, index) => {
1515
const { name, isActivated } = container.containerData[0];
16-
const containerData = getRequestsForContainer({ container, containers, containersPath: [], isPathActivated: isActivated, specVersion });
16+
const containerData = getRequestsForContainer({
17+
container,
18+
containers,
19+
containersPath: [],
20+
isPathActivated: isActivated,
21+
specVersion,
22+
});
1723

1824
if (!isActivated) {
19-
acc[`hackoladeCommentStart${index}`] = true;
25+
acc[`hackoladeCommentStart${index}`] = true;
2026
}
2127

2228
acc[name] = containerData;
2329

2430
if (!isActivated) {
25-
acc[`hackoladeCommentEnd${index}`] = true;
31+
acc[`hackoladeCommentEnd${index}`] = true;
2632
}
2733
return acc;
2834
}, {});
@@ -32,24 +38,41 @@ function getRequestsForContainer({ container, containers, containersPath = [], i
3238
const { contactExtensions, summary, description } = container.containerData[0];
3339

3440
const collections = container.entities.map(collectionId => JSON.parse(container.jsonSchema[collectionId]));
35-
const containerData = getRequestData({ collections, containers, containerId: container.id, containersPath, isPathActivated, specVersion });
41+
const containerData = getRequestData({
42+
collections,
43+
containers,
44+
containerId: container.id,
45+
containersPath,
46+
isPathActivated,
47+
specVersion,
48+
});
3649
const additionalContainerData = {
3750
summary,
38-
description: description || undefined
51+
description: description || undefined,
3952
};
4053

4154
const containerExtensions = getExtensions(contactExtensions);
4255

4356
return Object.assign({}, containerData, additionalContainerData, containerExtensions);
4457
}
4558

46-
function getRequestData({ collections, containers, containerId, containersPath = [], isPathActivated = true, specVersion }) {
59+
function getRequestData({
60+
collections,
61+
containers,
62+
containerId,
63+
containersPath = [],
64+
isPathActivated = true,
65+
specVersion,
66+
}) {
4767
return collections
4868
.filter(collection => collection.entityType === 'request')
4969
.map(data => {
5070
const isRequestActivated = data.isActivated && isPathActivated;
5171
const requestBodyPropKeyword = getRequestBodyPropKeyword(data.properties);
52-
let request = {
72+
const isRequestBodySupported = getIsRequestBodySupported({ collectionName: data.collectionName, specVersion });
73+
const extensions = getExtensions(data.scopesExtensions);
74+
75+
const request = {
5376
tags: commonHelper.mapArrayFieldByName(data.tags, 'tag'),
5477
summary: data.summary,
5578
description: data.description,
@@ -58,34 +81,28 @@ function getRequestData({ collections, containers, containerId, containersPath =
5881
parameters: mapRequestParameters({
5982
parameters: get(data, 'properties.parameters'),
6083
isParentActivated: isRequestActivated,
61-
specVersion
62-
})
63-
};
64-
const extensions = getExtensions(data.scopesExtensions);
65-
66-
if (!['get', 'delete'].includes(String(data.collectionName).toLowerCase())) {
67-
request.requestBody = mapRequestBody({
68-
data: get(data.properties, requestBodyPropKeyword),
69-
required: get(data, 'required', []).includes(requestBodyPropKeyword),
70-
isParentActivated: isRequestActivated,
71-
specVersion
72-
});
73-
}
74-
75-
request = {
76-
...request,
84+
specVersion,
85+
}),
86+
requestBody: isRequestBodySupported
87+
? mapRequestBody({
88+
data: get(data.properties, requestBodyPropKeyword),
89+
required: get(data, 'required', []).includes(requestBodyPropKeyword),
90+
isParentActivated: isRequestActivated,
91+
specVersion,
92+
})
93+
: undefined,
7794
responses: mapResponses({
7895
collections,
7996
collectionId: data.GUID,
8097
isRequestActivated,
81-
specVersion
98+
specVersion,
8299
}),
83100
callbacks: getCallbacks({
84101
data: get(data, 'properties.callbacks'),
85102
containers,
86103
containerId,
87104
containersPath,
88-
specVersion
105+
specVersion,
89106
}),
90107
deprecated: data.deprecated,
91108
security: commonHelper.mapSecurity(data.security),
@@ -94,19 +111,19 @@ function getRequestData({ collections, containers, containerId, containersPath =
94111
isActivated: data.isActivated,
95112
};
96113

97-
return Object.assign({}, request, extensions);
114+
return { ...request, ...extensions };
98115
})
99116
.reduce((acc, collection, index) => {
100117
const { methodName, isActivated } = collection;
101118
delete collection.methodName;
102119
delete collection.isActivated;
103120
const shouldCommentedFlagBeInserted = !isActivated && isPathActivated;
104121
if (shouldCommentedFlagBeInserted) {
105-
acc[`hackoladeCommentStart${index}`] = true;
122+
acc[`hackoladeCommentStart${index}`] = true;
106123
}
107124
acc[methodName] = collection;
108125
if (shouldCommentedFlagBeInserted) {
109-
acc[`hackoladeCommentEnd${index}`] = true;
126+
acc[`hackoladeCommentEnd${index}`] = true;
110127
}
111128
return acc;
112129
}, {});
@@ -120,7 +137,7 @@ function mapRequestParameters({ parameters, isParentActivated = false, specVersi
120137
if (!Array.isArray(parametersData)) {
121138
parametersData = [parametersData];
122139
}
123-
140+
124141
return parametersData.map(item => mapParameter({ data: item, required: false, isParentActivated, specVersion }));
125142
}
126143

@@ -135,15 +152,20 @@ function mapResponses({ collections, collectionId, isParentActivated, specVersio
135152
const responseCode = collection.collectionName;
136153
const shouldResponseBeCommented = !collection.isActivated && isParentActivated;
137154
const extensions = getExtensions(collection.scopesExtensions);
138-
const response = mapResponse({ data: get(collection, ['properties', Object.keys(collection.properties)[0]]), responseCollectionDescription: collection.description, shouldResponseBeCommented, specVersion });
155+
const response = mapResponse({
156+
data: get(collection, ['properties', Object.keys(collection.properties)[0]]),
157+
responseCollectionDescription: collection.description,
158+
shouldResponseBeCommented,
159+
specVersion,
160+
});
139161

140162
return { responseCode, response: { ...response, ...extensions } };
141163
})
142164
.reduce((acc, { responseCode, response }) => {
143165
acc[responseCode] = response;
144166
return acc;
145167
}, {});
146-
168+
147169
return responses;
148170
}
149171

@@ -158,7 +180,7 @@ function getCallbacks({ data, containers, containerId, containersPath = [], spec
158180
return;
159181
}
160182
if (hasRef(value)) {
161-
return { [key]: { [value.callbackExpression]: getRef(value, specVersion) }};
183+
return { [key]: { [value.callbackExpression]: getRef(value, specVersion) } };
162184
}
163185
const containerForCallback = containers.find(({ id }) => id === value.bucketId && id !== containerId);
164186
if (!containerForCallback) {
@@ -168,12 +190,11 @@ function getCallbacks({ data, containers, containerId, containersPath = [], spec
168190
container: containerForCallback,
169191
containers,
170192
containersPath: [...containersPath, containerId],
171-
specVersion
193+
specVersion,
172194
});
173195
const extensions = getExtensions(value.scopesExtensions);
174196

175-
return { [key]: { [value.callbackExpression]: callbackData, ...extensions }};
176-
197+
return { [key]: { [value.callbackExpression]: callbackData, ...extensions } };
177198
})
178199
.reduce((acc, item) => {
179200
acc = Object.assign({}, acc, item);
@@ -182,18 +203,18 @@ function getCallbacks({ data, containers, containerId, containersPath = [], spec
182203
}
183204

184205
function getRequestBodyPropKeyword(properties = {}) {
185-
const defaultKeyword = 'requestBody';
206+
const defaultKeyword = 'requestBody';
186207
const restRequestPropNames = ['parameters', 'callbacks'];
187208

188209
if (get(properties, defaultKeyword)) {
189210
return defaultKeyword;
190211
}
191212

192213
const requestBodyKey = Object.keys(properties).find(key => !restRequestPropNames.includes(key));
193-
return requestBodyKey
214+
return requestBodyKey;
194215
}
195216

196217
module.exports = {
197218
getPaths,
198-
getCallbacks
199-
};
219+
getCallbacks,
220+
};

0 commit comments

Comments
 (0)