Skip to content

Commit

Permalink
fix(1428): add support for multiValue
Browse files Browse the repository at this point in the history
  • Loading branch information
tplevko committed Sep 17, 2024
1 parent 9b7cd80 commit 10c31e5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/ui/src/models/camel-properties-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface CamelPropertyCommon {
displayName: string;
label?: string;
required: boolean;
multiValue?: boolean;
prefix?: string;
javaType: string;
enum?: string[];
autowired: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export abstract class AbstractCamelVisualEntity<T extends object> implements Bas

updateModel(path: string | undefined, value: unknown): void {
if (!path) return;
const updatedValue = CamelComponentSchemaService.getUriSerializedDefinition(path, value);
let updatedValue = CamelComponentSchemaService.getUriSerializedDefinition(path, value);
updatedValue = CamelComponentSchemaService.getMultiValueSerializedDefinition(path, updatedValue);

setValue(this.route, path, updatedValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ exports[`CamelComponentSchemaService getVisualComponentSchema should transform a
"parameters": {
"beanName": "myBean",
"method": "hello",
"parameters": {},
},
"uri": "bean",
},
Expand Down Expand Up @@ -935,6 +936,7 @@ exports[`CamelComponentSchemaService getVisualComponentSchema should transform a
"parameters": {
"beanName": "myBean",
"method": "hello",
"parameters": {},
},
"uri": "bean",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,43 @@ describe('CamelComponentSchemaService', () => {
});
});

describe('getMultiValueSerializedDefinition', () => {
it('should return the same parameters if the definition is not a component', () => {
const definition = { log: { message: 'Hello World' } };
const result = CamelComponentSchemaService.getMultiValueSerializedDefinition('from', definition);

expect(result).toEqual(definition);
});

it('should return the same parameters if the component is not found', () => {
const definition = {
uri: 'unknown-component',
parameters: { jobParameters: { test: 'test' }, triggerParameters: { test: 'test' } },
};
const result = CamelComponentSchemaService.getMultiValueSerializedDefinition('from', definition);

expect(result).toEqual(definition);
});

it('should query the catalog service', () => {
const definition = { uri: 'log', parameters: { message: 'Hello World' } };
const catalogServiceSpy = jest.spyOn(CamelCatalogService, 'getCatalogLookup');

CamelComponentSchemaService.getMultiValueSerializedDefinition('from', definition);
expect(catalogServiceSpy).toHaveBeenCalledWith('log');
});

it('should return the serialized definition', () => {
const definition = {
uri: 'quartz',
parameters: { jobParameters: { test: 'test' }, triggerParameters: { test: 'test' } },
};
const result = CamelComponentSchemaService.getMultiValueSerializedDefinition('from', definition);

expect(result).toEqual({ uri: 'quartz', parameters: { 'job.test': 'test', 'trigger.test': 'test' } });
});
});

describe('getUriSerializedDefinition', () => {
it('should return the same parameters if the definition is not a component', () => {
const definition = { log: { message: 'Hello World' } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,42 @@ export class CamelComponentSchemaService {
return definition;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
static getMultiValueSerializedDefinition(path: string, definition: any): ParsedParameters | undefined {
const camelElementLookup = this.getCamelComponentLookup(path, definition);
if (camelElementLookup.componentName === undefined) {
return definition;
}

const catalogLookup = CamelCatalogService.getCatalogLookup(camelElementLookup.componentName);
if (catalogLookup.catalogKind === CatalogKind.Component) {
const multiValueParameters: Map<string, string> = new Map<string, string>();
if (catalogLookup.definition?.properties !== undefined) {
Object.entries(catalogLookup.definition.properties).forEach(([key, value]) => {
if (value.multiValue) multiValueParameters.set(key, value.prefix!);
});
}
const defaultMultiValues: ParsedParameters = {};
const filteredParameters = definition.parameters;

if (definition.parameters !== undefined) {
Object.keys(definition.parameters).forEach((key) => {
if (multiValueParameters.has(key)) {
if (definition.parameters[key] === undefined) {
return;

Check warning on line 282 in packages/ui/src/models/visualization/flows/support/camel-component-schema.service.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/visualization/flows/support/camel-component-schema.service.ts#L282

Added line #L282 was not covered by tests
}
Object.keys(definition.parameters[key]).forEach((subKey) => {
defaultMultiValues[multiValueParameters.get(key) + subKey] = definition.parameters[key][subKey];
});
delete filteredParameters[key];
}
});
}
return Object.assign({}, definition, { parameters: { ...filteredParameters, ...defaultMultiValues } });
}
return definition;

Check warning on line 293 in packages/ui/src/models/visualization/flows/support/camel-component-schema.service.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/visualization/flows/support/camel-component-schema.service.ts#L293

Added line #L293 was not covered by tests
}

/**
* If the processor is a `from` or `to` processor, we need to extract the component name from the uri property
* and return both the processor name and the underlying component name to build the combined schema
Expand Down Expand Up @@ -385,11 +421,42 @@ export class CamelComponentSchemaService {
if (camelElementLookup.componentName !== undefined) {
updatedDefinition.parameters = updatedDefinition.parameters ?? {};
this.applyParametersFromSyntax(camelElementLookup.componentName, updatedDefinition);
this.readMultiValue(camelElementLookup.componentName, updatedDefinition);
}

return updatedDefinition;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static readMultiValue(componentName: string, definition: any) {
const catalogLookup = CamelCatalogService.getCatalogLookup(componentName);

const multiValueParameters: Map<string, string> = new Map<string, string>();
if (catalogLookup !== undefined && catalogLookup.definition?.properties !== undefined) {
Object.entries(catalogLookup.definition.properties).forEach(([key, value]) => {
if (value.multiValue) multiValueParameters.set(key, value.prefix!);
});
}
if (multiValueParameters.size > 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parameters: any = {};
const filteredParameters = definition.parameters;

for (const [key, value] of multiValueParameters) {
const nestParameters: ParsedParameters = {};

Object.entries(definition.parameters).forEach(([paramKey, paramValue]) => {
if (paramKey.startsWith(value)) {
nestParameters[paramKey.replace(value, '')] = paramValue as string;
delete filteredParameters[paramKey];

Check warning on line 451 in packages/ui/src/models/visualization/flows/support/camel-component-schema.service.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/models/visualization/flows/support/camel-component-schema.service.ts#L450-L451

Added lines #L450 - L451 were not covered by tests
}
parameters[key] = { ...nestParameters };
});
}
Object.assign(definition, { parameters: { ...filteredParameters, ...parameters } });
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static applyParametersFromSyntax(componentName: string, definition: any) {
const catalogLookup = CamelCatalogService.getCatalogLookup(componentName);
Expand Down

0 comments on commit 10c31e5

Please sign in to comment.