From 00c6d8a72920298c634f7fa5b3a5ea2cf4e8bf51 Mon Sep 17 00:00:00 2001 From: Mikhail Tseluiko Date: Fri, 8 Sep 2023 16:02:09 +0300 Subject: [PATCH] FE: fix issue with recursive collection references --- forward_engineering/api.js | 4 ++-- forward_engineering/helpers/udtHelper.js | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/forward_engineering/api.js b/forward_engineering/api.js index 6b41593..22201a7 100644 --- a/forward_engineering/api.js +++ b/forward_engineering/api.js @@ -151,9 +151,9 @@ const convertJsonToAvro = (jsonSchema, schemaName) => { const customProperties = getCustomProperties(getEntityLevelConfig(), jsonSchema); const schema = convertSchema(jsonSchema); const avroSchema = { - ...(_.isString(schema) ? { type: schema } : schema), + ...(!_.isString(schema) && schema), name: schemaName, - type: 'record', + type: (_.isString(schema) ? schema : 'record'), ...customProperties, }; diff --git a/forward_engineering/helpers/udtHelper.js b/forward_engineering/helpers/udtHelper.js index 4bac5ac..a36a797 100644 --- a/forward_engineering/helpers/udtHelper.js +++ b/forward_engineering/helpers/udtHelper.js @@ -197,7 +197,7 @@ const convertCollectionReferences = entities => { } const definition = entities.find(entity => entity.jsonSchema.GUID === field.ref).jsonSchema; - const definitionName = definition.code || definition.collectionName; + const definitionName = definition.code || definition.collectionName || definition.name; const subject = getConfluentSubjectName({ name: definitionName, @@ -217,7 +217,7 @@ const convertCollectionReferences = entities => { return { ...field, - $ref: `#/definitions/${definition.code || definition.collectionName}`, + $ref: `#/definitions/${definitionName}`, default: field.nullable ? null : field.default, }; }); @@ -235,20 +235,25 @@ const convertCollectionReferences = entities => { return { ...entity, jsonSchema, - references: filterReferencesByPath(references).map(reference => _.omit(reference, 'path')), + references: filterReferencesByPath(entity, references).map(reference => _.omit(reference, 'path')), }; }); return topologicalSort(entitiesWithReferences); }; -const filterReferencesByPath = references => references.filter(currentReference => { +const filterReferencesByPath = (entity, references) => references.filter(currentReference => { + const isRecursive = _.last(currentReference.path) === entity?.jsonSchema?.GUID; + if (isRecursive) { + return false; + } + const rootReference = references.find(reference => { - if (!reference.path || (reference.path.length >= currentReference.path.length)) { + if (!reference.path || (reference.path.length >= currentReference.path?.length)) { return false; } - return reference.path.every((path, index) => path === currentReference.path[index]); + return reference.path.every((path, index) => path === currentReference.path?.[index]); }); return !rootReference;