Skip to content

Commit c9b8da2

Browse files
committed
Domain references and de-activation of sections
1 parent 045c5cd commit c9b8da2

File tree

10 files changed

+175
-30
lines changed

10 files changed

+175
-30
lines changed

adapter/0.1.13.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright © 2016-2020 by IntegrIT S.A. dba Hackolade. All rights reserved.
3+
*
4+
* The copyright to the computer software herein is the property of IntegrIT S.A.
5+
* The software may be used and/or copied only with the written permission of
6+
* IntegrIT S.A. or in accordance with the terms and conditions stipulated in
7+
* the agreement/contract under which the software has been supplied.
8+
*
9+
* {
10+
* "add": {
11+
* "entity": [<names of new property>],
12+
* "container": [<names of new property>],
13+
* "model": [<names of new property>],
14+
* "view": [<names of new property>],
15+
* "field": {
16+
* "<type>": [<names of new property>]
17+
* }
18+
* },
19+
* "delete": {
20+
* "entity": [<names of new property>],
21+
* "container": [<names of new property>],
22+
* "model": [<names of new property>],
23+
* "view": [<names of new property>],
24+
* "field": {
25+
* "<type>": [<names of new property>]
26+
* }
27+
* },
28+
* "modify": {
29+
* "entity": [
30+
* {
31+
* "from": { <properties that identify record> },
32+
* "to": { <properties that need to be changed> }
33+
* }
34+
* ],
35+
* "container": [],
36+
* "model": [],
37+
* "view": [],
38+
* "field": []
39+
* },
40+
* }
41+
*/
42+
43+
{
44+
"add": {
45+
"container": ["isActivated"],
46+
"entity": ["isActivated"]
47+
}
48+
}

forward_engineering/api.js

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const yaml = require('js-yaml');
2+
const get = require('lodash.get');
23
const validationHelper = require('./helpers/validationHelper');
34
const getInfo = require('./helpers/infoHelper');
45
const { getPaths } = require('./helpers/pathHelper');
@@ -43,12 +44,21 @@ module.exports = {
4344
const resultSchema = Object.assign({}, openApiSchema, extensions);
4445

4546
switch (data.targetScriptOptions.format) {
46-
case 'yaml':
47-
cb(null, yaml.safeDump(resultSchema, { skipInvalid: true }));
47+
case 'yaml': {
48+
const schema = yaml.safeDump(resultSchema, { skipInvalid: true });
49+
const schemaWithComments = addCommentsSigns(schema, 'yaml');
50+
cb(null, schemaWithComments);
4851
break;
52+
}
4953
case 'json':
50-
default:
51-
cb(null, JSON.stringify(resultSchema, null, 2));
54+
default: {
55+
const schemaString = JSON.stringify(resultSchema, null, 2);
56+
let schema = addCommentsSigns(schemaString, 'json');
57+
if (!get(data, 'options.isCalledFromFETab')) {
58+
schema = removeCommentLines(schema);
59+
}
60+
cb(null, schema);
61+
}
5262
}
5363
} catch (err) {
5464
logger.log('error', { error: err }, 'OpenAPI FE Error');
@@ -58,17 +68,17 @@ module.exports = {
5868

5969
validate(data, logger, cb) {
6070
const { script, targetScriptOptions } = data;
61-
6271
try {
72+
const filteredScript = removeCommentLines(script);
6373
let parsedScript = {};
6474

6575
switch (targetScriptOptions.format) {
6676
case 'yaml':
67-
parsedScript = yaml.safeLoad(script);
77+
parsedScript = yaml.safeLoad(filteredScript);
6878
break;
6979
case 'json':
7080
default:
71-
parsedScript = JSON.parse(script);
81+
parsedScript = JSON.parse(filteredScript);
7282
}
7383

7484
validationHelper.validate(parsedScript)
@@ -85,3 +95,46 @@ module.exports = {
8595
}
8696
}
8797
};
98+
99+
const addCommentsSigns = (string, format) => {
100+
const commentsStart = /hackoladeCommentStart\d+/i;
101+
const commentsEnd = /hackoladeCommentEnd\d+/i;
102+
const innerCommentStart = /hackoladeInnerCommentStart/i;
103+
const innerCommentEnd = /hackoladeInnerCommentEnd/i;
104+
105+
const { result } = string.split('\n').reduce(({ isCommented, result }, line, index, array) => {
106+
if (commentsStart.test(line) || innerCommentStart.test(line)) {
107+
return { isCommented: true, result: result };
108+
}
109+
if (commentsEnd.test(line)) {
110+
return { isCommented: false, result };
111+
}
112+
if (innerCommentEnd.test(line)) {
113+
if (format === 'json') {
114+
array[index + 1] = '# ' + array[index + 1];
115+
}
116+
return { isCommented: false, result };
117+
}
118+
119+
const isNextLineInnerCommentStart = index + 1 < array.length && innerCommentStart.test(array[index + 1]);
120+
if (isCommented || isNextLineInnerCommentStart) {
121+
result = result + '# ' + line + '\n';
122+
} else {
123+
result = result + line + '\n';
124+
}
125+
126+
return { isCommented, result };
127+
}, { isCommented: false, result: '' });
128+
129+
return result;
130+
}
131+
132+
const removeCommentLines = (scriptString) => {
133+
const isCommentedLine = /^\s*#\s+/i;
134+
135+
return scriptString
136+
.split('\n')
137+
.filter(line => !isCommentedLine.test(line))
138+
.join('\n')
139+
.replace(/(.*?),\s*(\}|])/g, '$1$2');
140+
}

forward_engineering/helpers/componentsHelpers/responsesHelper.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function getResponses(data) {
2222
}, {});
2323
}
2424

25-
function mapResponse(data, responseCollectionDescription) {
25+
function mapResponse(data, responseCollectionDescription, shouldResponseBeCommented = false) {
2626
if (!data) {
2727
return;
2828
}
@@ -34,8 +34,18 @@ function mapResponse(data, responseCollectionDescription) {
3434
const content = getContent(get(data, `properties.content`));
3535
const links = getLinks(get(data, `properties.links`));
3636
const extensions = getExtensions(data.scopesExtensions);
37+
const response = {};
38+
if (shouldResponseBeCommented) {
39+
response[`hackoladeInnerCommentStart`] = true;
40+
}
41+
42+
Object.assign(response, { description, headers, content, links }, extensions);
43+
44+
if (shouldResponseBeCommented) {
45+
response[`hackoladeInnerCommentEnd`] = true;
46+
}
3747

38-
return Object.assign({}, { description, headers, content, links }, extensions);
48+
return response;
3949
}
4050

4151
module.exports = {

forward_engineering/helpers/pathHelper.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,28 @@ const { hasRef, getRef } = require('./typeHelper');
1010
function getPaths(containers, containersIdsForCallbacks = []) {
1111
return containers
1212
.filter(({ id }) => !containersIdsForCallbacks.includes(id))
13-
.reduce((acc, container) => {
14-
const { name } = container.containerData[0];
15-
const containerData = getRequestsForContainer(container, containers);
13+
.reduce((acc, container, index) => {
14+
const { name, isActivated } = container.containerData[0];
15+
const containerData = getRequestsForContainer(container, containers, [], isActivated);
16+
17+
if (!isActivated) {
18+
acc[`hackoladeCommentStart${index}`] = true;
19+
}
1620

1721
acc[name] = containerData;
22+
23+
if (!isActivated) {
24+
acc[`hackoladeCommentEnd${index}`] = true;
25+
}
1826
return acc;
1927
}, {});
2028
}
2129

22-
function getRequestsForContainer(container, containers, containersPath = []) {
30+
function getRequestsForContainer(container, containers, containersPath = [], isPathActivated = true) {
2331
const { contactExtensions, summary, description } = container.containerData[0];
2432

2533
const collections = container.entities.map(collectionId => JSON.parse(container.jsonSchema[collectionId]));
26-
const containerData = getRequestData(collections, containers, container.id, containersPath);
34+
const containerData = getRequestData(collections, containers, container.id, containersPath, isPathActivated);
2735
const additionalContainerData = {
2836
summary,
2937
description: description || undefined
@@ -34,7 +42,7 @@ function getRequestsForContainer(container, containers, containersPath = []) {
3442
return Object.assign({}, containerData, additionalContainerData, containerExtensions);
3543
}
3644

37-
function getRequestData(collections, containers, containerId, containersPath = []) {
45+
function getRequestData(collections, containers, containerId, containersPath = [], isPathActivated = true) {
3846
return collections
3947
.filter(collection => collection.entityType === 'request')
4048
.map(data => {
@@ -46,21 +54,30 @@ function getRequestData(collections, containers, containerId, containersPath = [
4654
operationId: data.operationId,
4755
parameters: mapRequestParameters(get(data, 'properties.parameters')),
4856
requestBody: mapRequestBody(get(data, 'properties.requestBody'), get(data, 'required', []).includes('requestBody')),
49-
responses: mapResponses(collections, data.GUID),
57+
responses: mapResponses(collections, data.GUID, isPathActivated && data.isActivated),
5058
callbacks: getCallbacks(get(data, 'properties.callbacks'), containers, containerId, containersPath),
5159
deprecated: data.deprecated,
5260
security: commonHelper.mapSecurity(data.security),
5361
servers: getServers(data.servers),
54-
methodName: data.collectionName
62+
methodName: data.collectionName,
63+
isActivated: data.isActivated
5564
};
5665
const extensions = getExtensions(data.operationExtensions);
5766

5867
return Object.assign({}, request, extensions);
5968
})
60-
.reduce((acc, collection) => {
61-
const { methodName } = collection;
69+
.reduce((acc, collection, index) => {
70+
const { methodName, isActivated } = collection;
6271
delete collection.methodName;
72+
delete collection.isActivated;
73+
const shouldCommentedFlagBeInserted = !isActivated && isPathActivated;
74+
if (shouldCommentedFlagBeInserted) {
75+
acc[`hackoladeCommentStart${index}`] = true;
76+
}
6377
acc[methodName] = collection;
78+
if (shouldCommentedFlagBeInserted) {
79+
acc[`hackoladeCommentEnd${index}`] = true;
80+
}
6481
return acc;
6582
}, {});
6683
}
@@ -76,7 +93,7 @@ function mapRequestParameters(parameters) {
7693
return [mapParameter(parameters.items)];
7794
}
7895

79-
function mapResponses(collections, collectionId) {
96+
function mapResponses(collections, collectionId, isParentActivated) {
8097
if (!collections || !collectionId) {
8198
return;
8299
}
@@ -85,13 +102,13 @@ function mapResponses(collections, collectionId) {
85102
.filter(collection => collection.entityType === 'response' && collection.parentCollection === collectionId)
86103
.map(collection => {
87104
const responseCode = collection.collectionName;
88-
const response = mapResponse(get(collection, 'properties.response'), collection.description);
105+
const shouldResponseBeCommented = !collection.isActivated && isParentActivated;
106+
const response = mapResponse(get(collection, 'properties.response'), collection.description, shouldResponseBeCommented);
89107

90108
return { responseCode, response };
91109
})
92110
.reduce((acc, { responseCode, response }) => {
93111
acc[responseCode] = response;
94-
95112
return acc;
96113
}, {});
97114

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "OpenAPI",
3-
"version": "0.1.12",
4-
"versionDate": "2020-01-03",
3+
"version": "0.1.13",
4+
"versionDate": "2020-01-16",
55
"author": "hackolade",
66
"engines": {
77
"hackolade": "3.3.0",

properties_pane/container_level/containerLevelConfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ making sure that you maintain a proper JSON format.
147147
"propertyType": "details",
148148
"template": "textarea"
149149
},
150+
{
151+
"propertyName": "Activated",
152+
"propertyKeyword": "isActivated",
153+
"propertyType": "checkbox",
154+
"propertyTooltip": "Deactivated item will be commented out in the schema"
155+
},
150156
{
151157
"propertyName": "summary",
152158
"propertyKeyword": "summary",

properties_pane/defaultData.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55
"dbVendor": "OpenAPI"
66
},
77
"container": {
8-
"name": "/path"
8+
"name": "/path",
9+
"isActivated": true
910
},
1011
"collection": {
1112
"collectionName": "get",
1213
"entityType": "request",
1314
"snippet": "requestStructure",
14-
"subtype": "requestBody"
15+
"subtype": "requestBody",
16+
"isActivated": true
1517
},
1618
"nestedCollection": {
1719
"collectionName": "200",
1820
"entityType": "response",
1921
"snippet": "responseStructure",
20-
"subtype": "response"
22+
"subtype": "response",
23+
"isActivated": true
2124
},
2225
"field": {
2326
"hackoladeMeta": {

properties_pane/entity_level/entityLevelConfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ making sure that you maintain a proper JSON format.
234234
"propertyType": "checkbox",
235235
"hidden": true
236236
},
237+
{
238+
"propertyName": "Activated",
239+
"propertyKeyword": "isActivated",
240+
"propertyType": "checkbox",
241+
"propertyTooltip": "Deactivated item will be commented out in the schema"
242+
},
237243
{
238244
"propertyName": "schema",
239245
"propertyKeyword": "pathItemSchema",

reverse_engineering/helpers/dataHelper.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ const handleRequestData = (requestData, request, fieldOrder) => {
710710
entityType: REQUEST,
711711
subtype: 'requestBody',
712712
collectionName: request,
713-
properties: requestSchema
713+
properties: requestSchema,
714+
isActivated: true
714715
});
715716
return { jsonSchema, responses };
716717
};
@@ -742,7 +743,8 @@ const handleResponseData = (responseObj, response, request, fieldOrder) => {
742743
parentCollection: request,
743744
properties: {
744745
response: responseData
745-
}
746+
},
747+
isActivated: true
746748
};
747749
return jsonSchema;
748750
};

reverse_engineering/helpers/resolveExternalDefinitionPathHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const resolvePath = (data, callback) => {
2323
return callback(null, `${bucket}/${request}${addPropertiesToPath(path.slice(3))}`);
2424
};
2525

26-
const addPropertiesToPath = path => path.length ? '/properties/' + path.join('/properties/') : '';
26+
const addPropertiesToPath = path => path.length ? '/properties/' + path.filter(item => item !== 'properties').join('/properties/') : '';
2727

2828
module.exports = {
2929
resolvePath

0 commit comments

Comments
 (0)