Skip to content

Commit

Permalink
HCK-9386: handle edge-case in FE when internal definition references …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Vitalii4as authored Jan 8, 2025
1 parent 8b1bc49 commit ee4e202
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
21 changes: 15 additions & 6 deletions forward_engineering/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
convertCollectionReferences,
resolveNamespaceReferences,
clearDefinitions,
resolveSchemaUdt,
} = require('./helpers/udtHelper');
const convertSchema = require('./helpers/convertJsonSchemaToAvro');
const {
Expand Down Expand Up @@ -49,7 +50,7 @@ const generateModelScript = (data, logger, cb, app) => {
clearDefinitions();
addDefinitions(convertedExternalDefinitions);
addDefinitions(convertedModelDefinitions);
setUserDefinedTypes(internalDefinitions);
setUserDefinedTypes(internalDefinitions, true);
resetDefinitionsUsage();

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

setUserDefinedTypes(externalDefinitions);
setUserDefinedTypes(modelDefinitions);
setUserDefinedTypes(internalDefinitions);
setUserDefinedTypes(internalDefinitions, true);
resetDefinitionsUsage();
const isFromUi = options.origin === 'ui';

Expand Down Expand Up @@ -209,11 +210,19 @@ const convertJsonToAvro = (jsonSchema, schemaName) => {
return resolveUdt(reorderAvroSchema(avroSchema));
};

const setUserDefinedTypes = definitions => {
addDefinitions(convertSchemaToUserDefinedTypes(definitions));
/**
* When we have a reference in the internal definitions that leads to a definition
* in the model definitions we need to resolve them to avoid creation of a UDT that references
* itself. It may happen when the definition have the same name as the reference.
*
* @param {Array<object>} definitions
* @param {boolean} [resolveReferences]
*/
const setUserDefinedTypes = (definitions, resolveReferences = false) => {
addDefinitions(convertSchemaToUserDefinedTypes(definitions, resolveReferences));
};

const convertSchemaToUserDefinedTypes = definitionsSchema => {
const convertSchemaToUserDefinedTypes = (definitionsSchema, resolveReferences) => {
definitionsSchema = parseJson(definitionsSchema);
const definitions = Object.keys(definitionsSchema.properties || {}).map(key => {
const definition = definitionsSchema.properties[key];
Expand All @@ -230,7 +239,7 @@ const convertSchemaToUserDefinedTypes = definitionsSchema => {
return definitions.reduce(
(result, { name, schema, customProperties, originalSchema }) => ({
...result,
[name]: { schema, customProperties, originalSchema },
[name]: { schema: resolveReferences ? resolveSchemaUdt(schema) : schema, customProperties, originalSchema },
}),
{},
);
Expand Down
15 changes: 2 additions & 13 deletions forward_engineering/helpers/udtHelper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { dependencies } = require('../../shared/appDependencies');
const _ = require('lodash');
const { isNamedType, filterAttributes } = require('../../shared/typeHelper');
const { AVRO_TYPES, SCRIPT_TYPES } = require('../../shared/constants');
const mapJsonSchema = require('../../shared/mapJsonSchema');
Expand All @@ -7,11 +7,9 @@ const mapAvroSchema = require('./mapAvroSchema');
const { getConfluentSubjectName } = require('./formatAvroSchemaByType');
const { prepareName } = require('./generalHelper');

let _;
let udt = {};

const getUdtItem = type => {
_ = dependencies.lodash;
if (!_.isString(type)) {
return;
}
Expand All @@ -22,8 +20,6 @@ const getUdtItem = type => {
};

const resolveUdt = avroSchema => {
_ = dependencies.lodash;

return mapAvroSchema(avroSchema, resolveSchemaUdt);
};

Expand Down Expand Up @@ -154,8 +150,6 @@ const convertNamedTypesToReferences = schema => {
};

const convertSchemaToReference = schema => {
_ = dependencies.lodash;

const referenceAttributes = filterAttributes(_.omit(schema, 'type'));

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

const resetDefinitionsUsage = () => {
_ = dependencies.lodash;

udt = Object.keys(udt || {}).reduce((updatedUdt, key) => {
const definition = udt[key];

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

const convertCollectionReferences = (entities, options) => {
_ = dependencies.lodash;

const entitiesIds = entities.map(entity => entity.jsonSchema.GUID);
const entitiesWithReferences = entities.map(entity => {
let references = [];
Expand Down Expand Up @@ -286,8 +276,6 @@ const filterReferencesByPath = (entity, references) =>
});

const resolveNamespaceReferences = entities => {
_ = dependencies.lodash;

const entitiesWithReferences = entities.map(entity => {
const mapper = mapJsonSchema(field => {
if (!field.ref) {
Expand Down Expand Up @@ -362,6 +350,7 @@ const getConfluentSchemaVersion = version => {

module.exports = {
resolveUdt,
resolveSchemaUdt,
getUdtItem,
addDefinitions,
clearDefinitions,
Expand Down

0 comments on commit ee4e202

Please sign in to comment.