Skip to content

Commit ee4e202

Browse files
authored
HCK-9386: handle edge-case in FE when internal definition references model definition and they both have the same name (#174)
* HCK-9386: handle edge-case in FE when internal definition references model definition and they both have the same name * Fix remarks
1 parent 8b1bc49 commit ee4e202

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

forward_engineering/api.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
convertCollectionReferences,
1313
resolveNamespaceReferences,
1414
clearDefinitions,
15+
resolveSchemaUdt,
1516
} = require('./helpers/udtHelper');
1617
const convertSchema = require('./helpers/convertJsonSchemaToAvro');
1718
const {
@@ -49,7 +50,7 @@ const generateModelScript = (data, logger, cb, app) => {
4950
clearDefinitions();
5051
addDefinitions(convertedExternalDefinitions);
5152
addDefinitions(convertedModelDefinitions);
52-
setUserDefinedTypes(internalDefinitions);
53+
setUserDefinedTypes(internalDefinitions, true);
5354
resetDefinitionsUsage();
5455

5556
const settings = getSettings({ containerData, entityData, modelData, references });
@@ -101,7 +102,7 @@ const generateScript = (data, logger, cb, app) => {
101102

102103
setUserDefinedTypes(externalDefinitions);
103104
setUserDefinedTypes(modelDefinitions);
104-
setUserDefinedTypes(internalDefinitions);
105+
setUserDefinedTypes(internalDefinitions, true);
105106
resetDefinitionsUsage();
106107
const isFromUi = options.origin === 'ui';
107108

@@ -209,11 +210,19 @@ const convertJsonToAvro = (jsonSchema, schemaName) => {
209210
return resolveUdt(reorderAvroSchema(avroSchema));
210211
};
211212

212-
const setUserDefinedTypes = definitions => {
213-
addDefinitions(convertSchemaToUserDefinedTypes(definitions));
213+
/**
214+
* When we have a reference in the internal definitions that leads to a definition
215+
* in the model definitions we need to resolve them to avoid creation of a UDT that references
216+
* itself. It may happen when the definition have the same name as the reference.
217+
*
218+
* @param {Array<object>} definitions
219+
* @param {boolean} [resolveReferences]
220+
*/
221+
const setUserDefinedTypes = (definitions, resolveReferences = false) => {
222+
addDefinitions(convertSchemaToUserDefinedTypes(definitions, resolveReferences));
214223
};
215224

216-
const convertSchemaToUserDefinedTypes = definitionsSchema => {
225+
const convertSchemaToUserDefinedTypes = (definitionsSchema, resolveReferences) => {
217226
definitionsSchema = parseJson(definitionsSchema);
218227
const definitions = Object.keys(definitionsSchema.properties || {}).map(key => {
219228
const definition = definitionsSchema.properties[key];
@@ -230,7 +239,7 @@ const convertSchemaToUserDefinedTypes = definitionsSchema => {
230239
return definitions.reduce(
231240
(result, { name, schema, customProperties, originalSchema }) => ({
232241
...result,
233-
[name]: { schema, customProperties, originalSchema },
242+
[name]: { schema: resolveReferences ? resolveSchemaUdt(schema) : schema, customProperties, originalSchema },
234243
}),
235244
{},
236245
);

forward_engineering/helpers/udtHelper.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { dependencies } = require('../../shared/appDependencies');
1+
const _ = require('lodash');
22
const { isNamedType, filterAttributes } = require('../../shared/typeHelper');
33
const { AVRO_TYPES, SCRIPT_TYPES } = require('../../shared/constants');
44
const mapJsonSchema = require('../../shared/mapJsonSchema');
@@ -7,11 +7,9 @@ const mapAvroSchema = require('./mapAvroSchema');
77
const { getConfluentSubjectName } = require('./formatAvroSchemaByType');
88
const { prepareName } = require('./generalHelper');
99

10-
let _;
1110
let udt = {};
1211

1312
const getUdtItem = type => {
14-
_ = dependencies.lodash;
1513
if (!_.isString(type)) {
1614
return;
1715
}
@@ -22,8 +20,6 @@ const getUdtItem = type => {
2220
};
2321

2422
const resolveUdt = avroSchema => {
25-
_ = dependencies.lodash;
26-
2723
return mapAvroSchema(avroSchema, resolveSchemaUdt);
2824
};
2925

@@ -154,8 +150,6 @@ const convertNamedTypesToReferences = schema => {
154150
};
155151

156152
const convertSchemaToReference = schema => {
157-
_ = dependencies.lodash;
158-
159153
const referenceAttributes = filterAttributes(_.omit(schema, 'type'));
160154

161155
return reorderAttributes({ ...referenceAttributes, type: schema.name });
@@ -170,8 +164,6 @@ const clearDefinitions = () => {
170164
};
171165

172166
const resetDefinitionsUsage = () => {
173-
_ = dependencies.lodash;
174-
175167
udt = Object.keys(udt || {}).reduce((updatedUdt, key) => {
176168
const definition = udt[key];
177169

@@ -180,8 +172,6 @@ const resetDefinitionsUsage = () => {
180172
};
181173

182174
const convertCollectionReferences = (entities, options) => {
183-
_ = dependencies.lodash;
184-
185175
const entitiesIds = entities.map(entity => entity.jsonSchema.GUID);
186176
const entitiesWithReferences = entities.map(entity => {
187177
let references = [];
@@ -286,8 +276,6 @@ const filterReferencesByPath = (entity, references) =>
286276
});
287277

288278
const resolveNamespaceReferences = entities => {
289-
_ = dependencies.lodash;
290-
291279
const entitiesWithReferences = entities.map(entity => {
292280
const mapper = mapJsonSchema(field => {
293281
if (!field.ref) {
@@ -362,6 +350,7 @@ const getConfluentSchemaVersion = version => {
362350

363351
module.exports = {
364352
resolveUdt,
353+
resolveSchemaUdt,
365354
getUdtItem,
366355
addDefinitions,
367356
clearDefinitions,

0 commit comments

Comments
 (0)