This is an automated email from the ASF dual-hosted git repository. marat pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-karavan.git
The following commit(s) were added to refs/heads/main by this push: new b711d505 Fix #1232 b711d505 is described below commit b711d505b76365988abd340944724c5b7b3a41e9 Author: Marat Gubaidullin <ma...@talismancloud.io> AuthorDate: Sun Apr 21 10:34:08 2024 -0400 Fix #1232 --- karavan-core/src/core/api/CamelDefinitionYaml.ts | 17 ++++++++++++++ karavan-core/test/beans.spec.ts | 30 ++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/karavan-core/src/core/api/CamelDefinitionYaml.ts b/karavan-core/src/core/api/CamelDefinitionYaml.ts index 6e2d14b9..88ed6071 100644 --- a/karavan-core/src/core/api/CamelDefinitionYaml.ts +++ b/karavan-core/src/core/api/CamelDefinitionYaml.ts @@ -72,6 +72,20 @@ export class CamelDefinitionYaml { return value === undefined || (value.trim && value.trim().length === 0); }; + static isEmptyObject(obj: any): boolean { + // Check if it's an object and not null + if (obj && typeof obj === 'object') { + // Get all enumerable property names + const keys = Object.keys(obj); + // Get all non-enumerable property names + const nonEnumProps = Object.getOwnPropertyNames(obj); + // Check if there are no properties + return keys.length === 0 && nonEnumProps.length === 0; + } + return false; + } + + static cleanupElement = (element: CamelElement, inArray?: boolean, inSteps?: boolean): CamelElement => { const result: any = {}; const object: any = { ...element }; @@ -90,6 +104,9 @@ export class CamelDefinitionYaml { if (object.properties && Object.keys(object.properties).length === 0) { delete object.properties; } + if (object.constructors && CamelDefinitionYaml.isEmptyObject(object.constructors)) { + delete object.constructors; + } } delete object.uuid; diff --git a/karavan-core/test/beans.spec.ts b/karavan-core/test/beans.spec.ts index 7e372f34..80b0d30e 100644 --- a/karavan-core/test/beans.spec.ts +++ b/karavan-core/test/beans.spec.ts @@ -18,7 +18,7 @@ import { expect } from 'chai'; import * as fs from 'fs'; import 'mocha'; import { CamelDefinitionYaml } from '../src/core/api/CamelDefinitionYaml'; -import { Integration } from '../lib/model/IntegrationDefinition'; +import { Beans, Integration } from '../lib/model/IntegrationDefinition'; import { RegistryBeanDefinition } from '../src/core/model/CamelDefinition'; import * as yaml from 'js-yaml'; @@ -38,6 +38,7 @@ describe('bean configuration', () => { expect(i.spec.flows[2].beans[0].properties['nested.foo']).to.equal('valueFoo'); expect(i.spec.flows[2].beans[1].name).to.equal('myProps'); } + CamelDefinitionYaml.integrationToYaml(i) }); it('Read beans from Integration', () => { @@ -55,8 +56,21 @@ describe('bean configuration', () => { } }); - class Val { + function countSubstring(str: string, search: string): number { + if (search.length === 0) { + return 0; // Avoid infinite loops for empty search strings + } + + let count = 0; + let pos = 0; + + // Loop to find all occurrences of 'search' + while ((pos = str.indexOf(search, pos)) !== -1) { + count++; // Increment count for each occurrence found + pos += search.length; // Move past the last found substring to find next + } + return count; } it('Bean constructor', () => { @@ -64,15 +78,17 @@ describe('bean configuration', () => { const i = CamelDefinitionYaml.yamlToIntegration('beans.yaml', text); const b = Integration.createNew('beans'); - const bean = new RegistryBeanDefinition({ - name: 'Name', type: 'Type', constructors: { + const bean1 = new RegistryBeanDefinition({ + name: 'Name1', type: 'Type', constructors: { 0: 'zero', 1: 'one', 2: 'two', - }, + } }); - b.spec.flows?.push(bean); - // console.log(CamelDefinitionYaml.integrationToYaml(b)) + const bean2 = new RegistryBeanDefinition({ name: 'Name2', type: 'Type'}); + b.spec.flows?.push(new Beans({beans: [bean1, bean2]})); + const yaml = CamelDefinitionYaml.integrationToYaml(b); + expect(countSubstring(yaml, 'constructors')).to.equal(1); }); });