Skip to content

Commit 35a70cb

Browse files
authored
Merge pull request #57 from taras-dubyk/feature/v3.1
Add OpenAPI 3.1.0 support (HCK-3044)
2 parents c520bb8 + 5c83d41 commit 35a70cb

File tree

917 files changed

+31651
-88520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

917 files changed

+31651
-88520
lines changed

forward_engineering/api.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ module.exports = {
1515
generateModelScript(data, logger, cb) {
1616
try {
1717
const {
18-
dbVersion,
18+
dbVersion: specVersion,
1919
externalDocs: modelExternalDocs,
2020
tags: modelTags,
2121
security: modelSecurity,
2222
servers: modelServers,
23+
jsonSchemaDialect,
2324
} = data.modelData[0];
2425

2526
const containersIdsFromCallbacks = commonHelper.getContainersIdsForCallbacks(data);
@@ -32,19 +33,23 @@ module.exports = {
3233
const servers = getServers(modelServers);
3334
const externalDefinitions = JSON.parse(data.externalDefinitions || '{}').properties || {};
3435
const containers = handleRefInContainers(data.containers, externalDefinitions, resolveApiExternalRefs);
35-
const paths = getPaths(containers, containersIdsFromCallbacks);
36+
const { pathContainers, webhookContainers } = separatePathAndWebhooks(containers);
37+
const paths = getPaths(pathContainers, containersIdsFromCallbacks, specVersion);
38+
const webhooks = getPaths(webhookContainers, containersIdsFromCallbacks, specVersion);
3639
const definitions = JSON.parse(data.modelDefinitions) || {};
3740
const definitionsWithHandledReferences = mapJsonSchema(definitions, handleRef(externalDefinitions, resolveApiExternalRefs));
38-
const components = getComponents(definitionsWithHandledReferences, data.containers);
41+
const components = getComponents({ definitions: definitionsWithHandledReferences, containers: data.containers, specVersion });
3942
const security = commonHelper.mapSecurity(modelSecurity);
4043
const tags = commonHelper.mapTags(modelTags);
4144
const externalDocs = commonHelper.mapExternalDocs(modelExternalDocs);
4245

4346
const openApiSchema = {
44-
openapi: dbVersion,
47+
openapi: specVersion,
4548
info,
49+
...(jsonSchemaDialect && { jsonSchemaDialect }),
4650
servers,
4751
paths,
52+
...(webhooks && Object.keys(webhooks).length ? { webhooks } : {}),
4853
components,
4954
security,
5055
tags,
@@ -212,3 +217,17 @@ const handleRef = (externalDefinitions, resolveApiExternalRefs) => field => {
212217

213218
return { ...field, ...ref };
214219
};
220+
221+
const separatePathAndWebhooks = containers => {
222+
const pathContainers = [];
223+
const webhookContainers = [];
224+
containers.forEach(container => {
225+
if (container.containerData?.[0]?.webhook) {
226+
webhookContainers.push(container);
227+
} else {
228+
pathContainers.push(container);
229+
}
230+
});
231+
232+
return { pathContainers, webhookContainers };
233+
}

forward_engineering/helpers/componentsHelpers/examplesHelper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const getExtensions = require('../extensionsHelper');
22
const { getRef, hasRef } = require('../typeHelper');
33

4-
function getExamples(data) {
4+
function getExamples(data, specVersion) {
55
if (!data || !data.properties) {
66
return;
77
}
@@ -10,7 +10,7 @@ function getExamples(data) {
1010
.map(([key, value]) => {
1111
return {
1212
key,
13-
value: mapExample(value)
13+
value: mapExample(value, specVersion)
1414
};
1515
})
1616
.reduce((acc, { key, value }) => {
@@ -19,12 +19,12 @@ function getExamples(data) {
1919
}, {});
2020
}
2121

22-
function mapExample(data) {
22+
function mapExample(data, specVersion) {
2323
if (!data) {
2424
return;
2525
}
2626
if (hasRef(data)) {
27-
return getRef(data);
27+
return getRef(data, specVersion);
2828
}
2929

3030
const { summary, description, value, externalValue, scopesExtensions } = data;

forward_engineering/helpers/componentsHelpers/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ const renameComponents = (components) => {
2222
}, {});
2323
};
2424

25-
function getComponents(definitions, containers) {
25+
function getComponents({ definitions, containers, specVersion }) {
2626
const componentsData = get(definitions, 'properties', {});
2727

28-
const schemas = renameComponents(getSchemas(componentsData.schemas));
29-
const responses = renameComponents(getResponses(componentsData.responses));
30-
const parameters = renameComponents(getParameters(componentsData.parameters));
31-
const examples = renameComponents(getExamples(componentsData.examples));
32-
const requestBodies = renameComponents(getRequestBodies(componentsData.requestBodies));
33-
const headers = renameComponents(getHeaders(prepareHeadersComponents(componentsData.headers), true));
34-
const securitySchemes = renameComponents(getSecuritySchemes(componentsData.securitySchemes));
35-
const links = renameComponents(getLinks(componentsData.links));
36-
const callbacks = renameComponents(getCallbacks(componentsData.callbacks, containers));
28+
const schemas = renameComponents(getSchemas(componentsData.schemas, specVersion));
29+
const responses = renameComponents(getResponses(componentsData.responses, specVersion));
30+
const parameters = renameComponents(getParameters(componentsData.parameters, specVersion));
31+
const examples = renameComponents(getExamples(componentsData.examples, specVersion));
32+
const requestBodies = renameComponents(getRequestBodies(componentsData.requestBodies, specVersion));
33+
const headers = renameComponents(getHeaders({ data: prepareHeadersComponents(componentsData.headers), isParentActivated: true, specVersion }));
34+
const securitySchemes = renameComponents(getSecuritySchemes(componentsData.securitySchemes, specVersion));
35+
const links = renameComponents(getLinks(componentsData.links, specVersion));
36+
const callbacks = renameComponents(getCallbacks({ data: componentsData.callbacks, containers, specVersion }));
3737

3838
const extensions = getExtensions(get(componentsData, `['Specification Extensions'].scopesExtensions`));
3939

forward_engineering/helpers/componentsHelpers/linksHelper.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { getRef, hasRef } = require('../typeHelper');
33
const getExtensions = require('../extensionsHelper');
44
const { mapServer } = require('../serversHelper');
55

6-
function getLinks(data) {
6+
function getLinks(data, specVersion) {
77
if (!data || !data.properties) {
88
return;
99
}
@@ -12,7 +12,7 @@ function getLinks(data) {
1212
.map(([key, value]) => {
1313
return {
1414
key,
15-
value: mapLink(value)
15+
value: mapLink(value, specVersion)
1616
};
1717
})
1818
.reduce((acc, { key, value }) => {
@@ -21,12 +21,12 @@ function getLinks(data) {
2121
}, {});
2222
}
2323

24-
function mapLink(data) {
24+
function mapLink(data, specVersion) {
2525
if (!data) {
2626
return;
2727
}
2828
if (hasRef(data)) {
29-
return getRef(data);
29+
return getRef(data, specVersion);
3030
}
3131

3232
const { operationRef, operationId, description, server, scopesExtensions } = data;
@@ -71,5 +71,4 @@ function mapParameters(data) {
7171

7272
module.exports = {
7373
getLinks,
74-
mapLink
7574
};

forward_engineering/helpers/componentsHelpers/parametersHelper.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ const { getRef, hasRef, hasChoice } = require('../typeHelper');
66
const { commentDeactivatedItemInner } = require('../commentsHelper');
77
const { activateItem } = require('../commonHelper');
88

9-
function getParameters(data) {
9+
function getParameters(data, specVersion) {
1010
if (!data || !data.properties) {
1111
return;
1212
}
1313

1414
return Object.entries(data.properties)
1515
.map(([key, value]) => {
1616
const required = data.required ? data.required.includes(key) : false;
17-
return { key, value: mapParameter(activateItem(value), required, true) };
17+
return { key, value: mapParameter({ data: activateItem(value), required, isParentActivated: true, specVersion }) };
1818
})
1919
.reduce((acc, { key, value }) => {
2020
acc[key] = value;
2121
return acc;
2222
}, {});
2323
}
2424

25-
function mapParameter(data, required, isParentActivated = false) {
25+
function mapParameter({ data, required, isParentActivated = false, specVersion}) {
2626
if (!data) {
2727
return;
2828
}
2929
if (hasRef(data)) {
30-
return commentDeactivatedItemInner(getRef(data), data.isActivated, isParentActivated);
30+
return commentDeactivatedItemInner(getRef(data, specVersion), data.isActivated, isParentActivated);
3131
}
3232
const schemaKeyword = getSchemaKeyword(data.properties);
3333
const isActivated = data.isActivated && isParentActivated;
@@ -41,10 +41,10 @@ function mapParameter(data, required, isParentActivated = false) {
4141
style: data.style,
4242
explode: data.explode,
4343
allowReserved: data.allowReserved,
44-
schema: mapSchema(get(data, ['properties', schemaKeyword]), 'schema', isActivated),
44+
schema: mapSchema({ data: get(data, ['properties', schemaKeyword]), key: 'schema', isParentActivated: isActivated, specVersion }),
4545
example: data.sample,
46-
examples: getExamples(get(data, 'properties.examples')),
47-
content: getContent(get(data, 'properties.content'), isActivated)
46+
examples: getExamples(get(data, 'properties.examples'), specVersion),
47+
content: getContent({ data: get(data, 'properties.content'), isParentActivated: isActivated, specVersion })
4848
};
4949
const extensions = getExtensions(data.scopesExtensions);
5050

@@ -62,7 +62,7 @@ function getIn(parameterType) {
6262
return parameterTypeToIn[parameterType];
6363
}
6464

65-
function getHeaders(data, isParentActivated = false) {
65+
function getHeaders({ data, isParentActivated = false, specVersion }) {
6666
if (!data || !data.properties) {
6767
return;
6868
}
@@ -72,7 +72,7 @@ function getHeaders(data, isParentActivated = false) {
7272
const isActivated = value.isActivated;
7373
return {
7474
key,
75-
value: mapHeader(value, isActivated && isParentActivated),
75+
value: mapHeader({ data: value, isParentActivated: isActivated && isParentActivated, specVersion }),
7676
isActivated
7777
};
7878
})
@@ -82,19 +82,19 @@ function getHeaders(data, isParentActivated = false) {
8282
}, {});
8383
}
8484

85-
function mapHeader(data, isParentActivated = false) {
85+
function mapHeader({ data, isParentActivated = false, specVersion }) {
8686
if (!data) {
8787
return;
8888
}
8989
if (hasRef(data)) {
90-
return commentDeactivatedItemInner(getRef(data), data.isActivated, isParentActivated);
90+
return commentDeactivatedItemInner(getRef(data, specVersion), data.isActivated, isParentActivated);
9191
}
9292

9393
delete data.parameterName;
94-
return mapParameter(data, false, isParentActivated);
94+
return mapParameter({ data, required: false, isParentActivated, specVersion });
9595
}
9696

97-
function getContent(data, isParentActivated) {
97+
function getContent({ data, isParentActivated, specVersion }) {
9898
if (!data || !data.properties) {
9999
return;
100100
}
@@ -117,10 +117,11 @@ function getContent(data, isParentActivated) {
117117
}
118118
const isActivated = data.properties[key].isActivated;
119119
acc[key] = commentDeactivatedItemInner(
120-
mapMediaTypeObject(
121-
data.properties[key],
122-
isActivated && isParentActivated
123-
),
120+
mapMediaTypeObject({
121+
data: data.properties[key],
122+
isParentActivated: isActivated && isParentActivated,
123+
specVersion
124+
}),
124125
isActivated,
125126
isParentActivated
126127
);
@@ -134,23 +135,23 @@ function getContent(data, isParentActivated) {
134135
return result;
135136
}
136137

137-
function mapMediaTypeObject(data, isParentActivated = false) {
138+
function mapMediaTypeObject({ data, isParentActivated = false, specVersion }) {
138139
if (!data || !data.properties) {
139140
return;
140141
}
141142
const schemaKeyword = getSchemaKeyword(data.properties);
142-
let schema = mapSchema(get(data, ['properties', schemaKeyword]), 'schema', isParentActivated);
143+
let schema = mapSchema({ data: get(data, ['properties', schemaKeyword]), key: 'schema', isParentActivated, specVersion });
143144
if (!schema && hasChoice(data)) {
144-
schema = mapSchema({
145+
schema = mapSchema({ data: {
145146
type: 'object',
146147
allOf: data.allOf,
147148
oneOf: data.oneOf,
148149
anyOf: data.anyOf,
149150
not: data.not
150-
}, 'schema', isParentActivated);
151+
}, key: 'schema', isParentActivated, specVersion });
151152
}
152-
const examples = getExamples(get(data, 'properties.examples'));
153-
const encoding = mapEncoding(get(data, 'properties.encoding'));
153+
const examples = getExamples(get(data, 'properties.examples'), specVersion);
154+
const encoding = mapEncoding({ data: get(data, 'properties.encoding'), specVersion });
154155
let example;
155156
try {
156157
example = JSON.parse(data.sample);
@@ -163,7 +164,7 @@ function mapMediaTypeObject(data, isParentActivated = false) {
163164
return Object.assign({}, mediaTypeObj, extensions);
164165
}
165166

166-
function mapEncoding(data) {
167+
function mapEncoding({ data, specVersion }) {
167168
if (!data || !data.properties) {
168169
return;
169170
}
@@ -172,7 +173,7 @@ function mapEncoding(data) {
172173
.map(([key, value]) => {
173174
const encodingObj = {
174175
contentType: value.contentType,
175-
headers: getHeaders(get(value, 'properties.headers')),
176+
headers: getHeaders({ data: get(value, 'properties.headers'), isParentActivated: value.isActivated, specVersion }),
176177
style: value.style,
177178
explode: value.explode || undefined,
178179
allowReserved: value.allowReserved || undefined
@@ -215,7 +216,6 @@ module.exports = {
215216
getParameters,
216217
mapParameter,
217218
getHeaders,
218-
mapHeader,
219219
getContent,
220220
prepareHeadersComponents
221221
};

forward_engineering/helpers/componentsHelpers/requestBodiesHelper.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { getRef, hasRef } = require('../typeHelper');
55
const { commentDeactivatedItemInner } = require('../commentsHelper');
66
const { activateItem } = require('../commonHelper');
77

8-
function getRequestBodies(data) {
8+
function getRequestBodies(data, specVersion) {
99
if (!data || !data.properties) {
1010
return;
1111
}
@@ -14,7 +14,7 @@ function getRequestBodies(data) {
1414
.map(([key, value]) => {
1515
return {
1616
key,
17-
value: mapRequestBody(activateItem(value), get(data, 'required', []).includes(key), true)
17+
value: mapRequestBody({ data: activateItem(value), required: get(data, 'required', []).includes(key), isParentActivated: true, specVersion }),
1818
};
1919
})
2020
.reduce((acc, { key, value }) => {
@@ -23,15 +23,15 @@ function getRequestBodies(data) {
2323
}, {});
2424
}
2525

26-
function mapRequestBody(data, required, isParentActivated = false) {
26+
function mapRequestBody({ data, required, isParentActivated = false, specVersion }) {
2727
if (!data) {
2828
return;
2929
}
3030
if (hasRef(data)) {
31-
return commentDeactivatedItemInner(getRef(data), data.isActivated, isParentActivated);
31+
return commentDeactivatedItemInner(getRef(data, specVersion), data.isActivated, isParentActivated);
3232
}
3333

34-
const content = getContent(data, data.isActivated && isParentActivated);
34+
const content = getContent({ data, isParentActivated: data.isActivated && isParentActivated, specVersion });
3535
if (!content) {
3636
return;
3737
}

0 commit comments

Comments
 (0)