Skip to content

Commit f180b40

Browse files
authored
HCK-14077: fix FE of union fields generated from choices (#205)
1 parent 288146c commit f180b40

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

forward_engineering/helpers/convertChoicesToProperties.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,55 @@ const convertChoiceToProperties = (schema, choice) => {
5353
// custom properties of choice have higher priority than custom properties of fields in subschemas
5454
const choiceCustomProperties = getFieldCustomProperties({ schema: { ...choiceMeta, type: 'choice' } });
5555

56+
const choiceName =
57+
choiceMeta.code ||
58+
choiceMeta.name ||
59+
allSubSchemaFields[0]?.code ||
60+
allSubSchemaFields[0]?.name ||
61+
getDefaultName();
62+
63+
const fieldWithDescription = allSubSchemaFields.findLast(field => field.description || field.refDescription);
64+
const choiceDescription =
65+
choiceMeta.description || fieldWithDescription?.description || fieldWithDescription?.refDescription;
66+
5667
const multipleFieldsHash = allSubSchemaFields.reduce((multipleFieldsHash, field, index) => {
57-
const fieldName = choiceMeta.code || choiceMeta.name || field.name || getDefaultName();
58-
const fieldDescription = choiceMeta.description || field.description || field.refDescription;
59-
const multipleField = multipleFieldsHash[fieldName] || {
68+
const multipleField = multipleFieldsHash[choiceName] || {
6069
...choiceMeta,
6170
default: convertDefaultMetaFieldType(field.type, choiceMeta.default),
62-
name: prepareName(fieldName),
71+
name: prepareName(choiceName),
6372
type: [],
6473
choiceMeta,
6574
};
6675
const multipleTypeAttributes = {
6776
...field,
68-
...(fieldDescription && { description: fieldDescription }),
77+
// When the choice have the description property, we show it and ignore the property in the fields
78+
// when the field is not a reference. If it is a reference, the field can have the description of the
79+
// but it will come from the definition and its not handled here.
80+
//
81+
// When the choice have no description we take the description of a field and put it into choice, at the same
82+
// time removing it from field.
83+
description: undefined,
6984
type: field.$ref ? getTypeFromReference(field) : field.type,
70-
name: prepareName(field.name || fieldName),
85+
name: prepareName(field.code || field.name || choiceName),
7186
};
72-
const multipleTypes = filterMultipleTypes(ensureArray(multipleField.type).concat(multipleTypeAttributes));
87+
const multipleTypes = ensureArray(multipleField.type).concat(multipleTypeAttributes);
7388
const type = _.isArray(multipleTypes)
74-
? multipleTypes.map(typeSchema => typeSchema?.type || typeSchema)
89+
? multipleTypes.map(typeSchema =>
90+
['fixed', 'enum', 'record'].includes(typeSchema?.type)
91+
? typeSchema
92+
: typeSchema?.type || typeSchema,
93+
)
7594
: multipleTypes?.type || multipleTypes;
7695
const defaultFromSubschema = index === 0 ? multipleTypeAttributes.default : undefined;
7796
const defaultValue = !_.isUndefined(multipleField.default) ? multipleField.default : defaultFromSubschema;
7897

7998
return {
8099
...multipleFieldsHash,
81-
[fieldName]: {
100+
[choiceName]: {
82101
...convertName(multipleField),
83102
...convertName(multipleTypeAttributes),
84103
...choiceCustomProperties,
104+
...(choiceDescription && { description: choiceDescription }),
85105
default: defaultValue,
86106
type,
87107
},

forward_engineering/helpers/generalHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const setPropertyAsFirst = key => avroSchema => {
4040
};
4141

4242
const filterMultipleTypes = schemaTypes => {
43-
const types = _.uniqBy(schemaTypes, type => type?.type || type);
43+
const types = _.uniqBy(schemaTypes, type => type);
4444
if (types.length === 1) {
4545
return _.first(types);
4646
}

0 commit comments

Comments
 (0)