Skip to content

Commit 050a6ab

Browse files
committed
handle discriminator object
1 parent 837edb0 commit 050a6ab

File tree

3 files changed

+59
-37
lines changed

3 files changed

+59
-37
lines changed

forward_engineering/helpers/typeHelper.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function getTypeProps(data, key, isParentActivated) {
3636
maxItems: data.maxItems,
3737
uniqueItems: data.uniqueItems || undefined,
3838
nullable: data.nullable,
39-
discriminator: data.discriminator,
4039
readOnly: data.readOnly,
4140
example: parseExample(data.sample) || getArrayItemsExample(items),
4241
xml: getXml(data.xml)
@@ -46,6 +45,7 @@ function getTypeProps(data, key, isParentActivated) {
4645
return Object.assign({}, arrayProps, arrayChoices, extensions);
4746
}
4847
case 'object': {
48+
const discriminator = getDiscriminator(data.discriminator);
4949
const objectProps = {
5050
type,
5151
title: data.title || undefined,
@@ -56,7 +56,7 @@ function getTypeProps(data, key, isParentActivated) {
5656
maxProperties: data.maxProperties,
5757
additionalProperties: getAdditionalProperties(data),
5858
nullable: data.nullable,
59-
discriminator: data.discriminator,
59+
...(discriminator ? { discriminator } : {}),
6060
readOnly: data.readOnly,
6161
example: parseExample(data.sample),
6262
xml: getXml(data.xml)
@@ -239,6 +239,22 @@ function getArrayItemsExample(items) {
239239
}
240240
}
241241

242+
function getDiscriminator(discriminator) {
243+
if (!discriminator || !discriminator.propertyName) {
244+
return null;
245+
}
246+
const mapping = (!discriminator.mapping || discriminator.mapping.length === 0) ? null : discriminator.mapping.reduce((acc, mappingItem) => {
247+
acc[mappingItem.discriminatorValue] = mappingItem.discriminatorSchema;
248+
return acc;
249+
}, {});
250+
251+
return {
252+
propertyName: discriminator.propertyName,
253+
...(mapping && { mapping })
254+
255+
};
256+
}
257+
242258
module.exports = {
243259
getType,
244260
getRef,

properties_pane/field_level/fieldLevelConfig.json

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,6 @@ making sure that you maintain a proper JSON format.
328328
"propertyType": "checkbox",
329329
"enableForReference": true
330330
},
331-
{
332-
"propertyName": "discriminator",
333-
"propertyKeyword": "discriminator",
334-
"propertyType": "text",
335-
"sampleGen": "&containerName|&entityName|&random|<value>",
336-
"enableForReference": true
337-
},
338331
{
339332
"propertyName": "readOnly",
340333
"propertyKeyword": "readOnly",
@@ -607,13 +600,6 @@ making sure that you maintain a proper JSON format.
607600
"propertyType": "checkbox",
608601
"enableForReference": true
609602
},
610-
{
611-
"propertyName": "discriminator",
612-
"propertyKeyword": "discriminator",
613-
"propertyType": "text",
614-
"sampleGen": "&containerName|&entityName|&random|<value>",
615-
"enableForReference": true
616-
},
617603
{
618604
"propertyName": "readOnly",
619605
"propertyKeyword": "readOnly",
@@ -887,13 +873,6 @@ making sure that you maintain a proper JSON format.
887873
"propertyType": "checkbox",
888874
"enableForReference": true
889875
},
890-
{
891-
"propertyName": "discriminator",
892-
"propertyKeyword": "discriminator",
893-
"propertyType": "text",
894-
"sampleGen": "&containerName|&entityName|&random|<value>",
895-
"enableForReference": true
896-
},
897876
{
898877
"propertyName": "readOnly",
899878
"propertyKeyword": "readOnly",
@@ -1273,17 +1252,6 @@ making sure that you maintain a proper JSON format.
12731252
"value": "schema"
12741253
}
12751254
},
1276-
{
1277-
"propertyName": "discriminator",
1278-
"propertyKeyword": "discriminator",
1279-
"propertyType": "text",
1280-
"sampleGen": "&containerName|&entityName|&random|<value>",
1281-
"enableForReference": true,
1282-
"dependency": {
1283-
"key": "subtype",
1284-
"value": "schema"
1285-
}
1286-
},
12871255
{
12881256
"propertyName": "readOnly",
12891257
"propertyKeyword": "readOnly",
@@ -1592,10 +1560,34 @@ making sure that you maintain a proper JSON format.
15921560
}
15931561
},
15941562
{
1595-
"propertyName": "discriminator",
1563+
"propertyName": "Discriminator",
15961564
"propertyKeyword": "discriminator",
1597-
"propertyType": "text",
1598-
"sampleGen": "&containerName|&entityName|&random|<value>",
1565+
"propertyType": "block",
1566+
"structure": [
1567+
{
1568+
"propertyName": "Property name",
1569+
"propertyKeyword": "propertyName",
1570+
"propertyTooltip": "",
1571+
"propertyType": "text"
1572+
},
1573+
{
1574+
"propertyName": "Mapping",
1575+
"propertyType": "group",
1576+
"propertyKeyword": "mapping",
1577+
"structure": [
1578+
{
1579+
"propertyName": "Discriminator value",
1580+
"propertyKeyword": "discriminatorValue",
1581+
"propertyType": "text"
1582+
},
1583+
{
1584+
"propertyName": "Schema name or reference",
1585+
"propertyKeyword": "discriminatorSchema",
1586+
"propertyType": "text"
1587+
}
1588+
]
1589+
}
1590+
],
15991591
"dependency": {
16001592
"key": "subtype",
16011593
"value": "schema"

reverse_engineering/helpers/dataHelper.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ const handleSchemaProperty = (property, data) => {
545545
return handleSchemaXml(data);
546546
case '$ref':
547547
return resolveReference(data);
548+
case 'discriminator':
549+
return handleDiscriminator(data);
548550
default:
549551
if (commonHelper.CHOICES.find(choice => data[choice])) {
550552
return handleSchemaProps(data);
@@ -920,6 +922,18 @@ const validateOpenAPISchema = (schema) => {
920922
return isCorrectVersion;
921923
};
922924

925+
const handleDiscriminator = (discriminator) => {
926+
if (discriminator) {
927+
const { propertyName, mapping } = discriminator;
928+
const preparedMapping = mapping ? Object.keys(mapping).reduce((acc, key) => {
929+
acc.push({ discriminatorValue: key, discriminatorSchema: mapping[key] });
930+
return acc;
931+
}, []) : [];
932+
return { propertyName, mapping: preparedMapping };
933+
}
934+
return {};
935+
};
936+
923937
module.exports = {
924938
getModelData,
925939
getComponents,

0 commit comments

Comments
 (0)