Skip to content

Commit 9cc7109

Browse files
committed
refactor(kamelet): update Kamelet structure to use 'route' instead of 'from'
Kamelets support both `route.from` and `from` definitions. At the moment, Kaoto supports `from` only, leaving other types of Kamelet without support. This commit reads both Kamelet's type but serializes it to the more expressive one to keep backward compatibility
1 parent 070a3f9 commit 9cc7109

11 files changed

Lines changed: 242 additions & 186 deletions

packages/ui/src/models/__snapshots__/kaoto-resource.test.ts.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
exports[`CamelResourceFactory.createCamelResource > should create an empty KameletResource if no args is specified 1`] = `
44
[
55
{
6-
"from": {},
6+
"route": {
7+
"from": {},
8+
},
79
},
810
]
911
`;

packages/ui/src/models/camel/__snapshots__/kamelet-resource.test.ts.snap

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,28 @@ exports[`KameletResource > should convert to JSON 1`] = `
3838
],
3939
"template": {
4040
"beans": undefined,
41-
"from": {
42-
"id": "from-1234",
43-
"parameters": {
44-
"period": "{{period}}",
45-
"timerName": "user",
46-
},
47-
"steps": [
48-
{
49-
"to": {
50-
"parameters": {
51-
"httpUri": "random-data-api.com/api/v2/users",
41+
"route": {
42+
"from": {
43+
"id": "from-1234",
44+
"parameters": {
45+
"period": "{{period}}",
46+
"timerName": "user",
47+
},
48+
"steps": [
49+
{
50+
"to": {
51+
"parameters": {
52+
"httpUri": "random-data-api.com/api/v2/users",
53+
},
54+
"uri": "https",
5255
},
53-
"uri": "https",
5456
},
55-
},
56-
{
57-
"to": "kamelet:sink",
58-
},
59-
],
60-
"uri": "timer",
57+
{
58+
"to": "kamelet:sink",
59+
},
60+
],
61+
"uri": "timer",
62+
},
6163
},
6264
},
6365
"types": {
@@ -107,26 +109,28 @@ exports[`KameletResource > should create a new KameletResource 1`] = `
107109
],
108110
"template": {
109111
"beans": undefined,
110-
"from": {
111-
"id": "from-1234",
112-
"parameters": {
113-
"period": "{{period}}",
114-
"timerName": "user",
115-
},
116-
"steps": [
117-
{
118-
"to": {
119-
"parameters": {
120-
"httpUri": "random-data-api.com/api/v2/users",
112+
"route": {
113+
"from": {
114+
"id": "from-1234",
115+
"parameters": {
116+
"period": "{{period}}",
117+
"timerName": "user",
118+
},
119+
"steps": [
120+
{
121+
"to": {
122+
"parameters": {
123+
"httpUri": "random-data-api.com/api/v2/users",
124+
},
125+
"uri": "https",
121126
},
122-
"uri": "https",
123127
},
124-
},
125-
{
126-
"to": "kamelet:sink",
127-
},
128-
],
129-
"uri": "timer",
128+
{
129+
"to": "kamelet:sink",
130+
},
131+
],
132+
"uri": "timer",
133+
},
130134
},
131135
},
132136
"types": {
@@ -163,10 +167,12 @@ exports[`KameletResource > should create a new KameletResource with a kamelet 1`
163167
"dependencies": [],
164168
"template": {
165169
"beans": [],
166-
"from": {
167-
"id": "from",
168-
"steps": [],
169-
"uri": "kamelet:source",
170+
"route": {
171+
"from": {
172+
"id": "from",
173+
"steps": [],
174+
"uri": "kamelet:source",
175+
},
170176
},
171177
},
172178
},
@@ -176,26 +182,28 @@ exports[`KameletResource > should create a new KameletResource with a kamelet 1`
176182
exports[`KameletResource > should get the visual entities (Camel Route Visual Entity) 1`] = `
177183
[
178184
{
179-
"from": {
180-
"id": "from-1234",
181-
"parameters": {
182-
"period": "{{period}}",
183-
"timerName": "user",
184-
},
185-
"steps": [
186-
{
187-
"to": {
188-
"parameters": {
189-
"httpUri": "random-data-api.com/api/v2/users",
185+
"route": {
186+
"from": {
187+
"id": "from-1234",
188+
"parameters": {
189+
"period": "{{period}}",
190+
"timerName": "user",
191+
},
192+
"steps": [
193+
{
194+
"to": {
195+
"parameters": {
196+
"httpUri": "random-data-api.com/api/v2/users",
197+
},
198+
"uri": "https",
190199
},
191-
"uri": "https",
192200
},
193-
},
194-
{
195-
"to": "kamelet:sink",
196-
},
197-
],
198-
"uri": "timer",
201+
{
202+
"to": "kamelet:sink",
203+
},
204+
],
205+
"uri": "timer",
206+
},
199207
},
200208
},
201209
]

packages/ui/src/models/camel/kamelet-resource.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ describe('KameletResource', () => {
4343
},
4444
dependencies: [],
4545
template: {
46-
from: {
47-
id: 'from',
48-
uri: 'kamelet:source',
49-
steps: [],
50-
},
5146
beans: [],
47+
route: {
48+
from: {
49+
id: 'from',
50+
uri: 'kamelet:source',
51+
steps: [],
52+
},
53+
},
5254
},
5355
},
5456
});

packages/ui/src/models/camel/kamelet-resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class KameletResource extends CamelKResource implements RouteTemplateBean
6969
// Call toJSON() on the flow entity to apply property sorting
7070
const flowJson = this.flow.toJSON();
7171
setValue(this.resource, 'metadata.name', this.flow.getId());
72-
setValue(this.resource, 'spec.template.from', flowJson.from);
72+
setValue(this.resource, 'spec.template.route', flowJson.route);
7373
setValue(this.resource, 'spec.template.beans', this.beans?.parent.beans);
7474
return this.resource as IKameletDefinition;
7575
}

packages/ui/src/models/camel/kamelets-catalog.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BeanFactory, FromDefinition, Kamelet } from '@kaoto/camel-catalog/types';
1+
import { BeanFactory, FromDefinition, Kamelet, RouteDefinition } from '@kaoto/camel-catalog/types';
22

33
import { KaotoSchemaDefinition } from '../kaoto-schema';
44
import { SourceSchemaType } from './source-schema-type';
@@ -49,7 +49,8 @@ export interface IKameletSpec {
4949
dependencies: string[];
5050
template: {
5151
beans?: BeanFactory[];
52-
from: FromDefinition;
52+
route: RouteDefinition;
53+
from?: FromDefinition;
5354
};
5455
dataTypes?: {
5556
in?: {

packages/ui/src/models/visualization/flows/kamelet-visual-entity.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ describe('KameletVisualEntity', () => {
5454
type: 'source',
5555
},
5656
template: {
57-
from: camelFromJson.from,
57+
route: {
58+
from: camelFromJson.from,
59+
},
5860
},
5961
dependencies: [],
6062
},
@@ -65,6 +67,12 @@ describe('KameletVisualEntity', () => {
6567
expect(new KameletVisualEntity(kameletDef)).toBeTruthy();
6668
});
6769

70+
it('should normalize template.from to template.route.from and remove template.from', () => {
71+
const kameletVisualEntity = new KameletVisualEntity(kameletDef);
72+
expect(kameletVisualEntity.kamelet.spec.template.from).toBeUndefined();
73+
expect(kameletVisualEntity.kamelet.spec.template.route?.from).toEqual(camelFromJson.from);
74+
});
75+
6876
it('should set the id to the name if provided', () => {
6977
const kameletVisualEntity = new KameletVisualEntity(kameletDef);
7078
expect(kameletVisualEntity.id).toBe('My Kamelet');
@@ -138,12 +146,27 @@ describe('KameletVisualEntity', () => {
138146
expect(fetchNodeSchemaSpy).toHaveBeenCalledWith(ids);
139147
});
140148

141-
it('should return the root uri', () => {
149+
it('should return the root uri when using template.from (short syntax)', () => {
150+
class KameletVisualEntityTest extends KameletVisualEntity {
151+
getRootUri(): string | undefined {
152+
return super.getRootUri();
153+
}
154+
}
155+
const kamelet = new KameletVisualEntityTest(kameletDef);
156+
expect(kamelet.getRootUri()).toBe('timer');
157+
});
158+
159+
it('should return the root uri when using template.route.from', () => {
142160
class KameletVisualEntityTest extends KameletVisualEntity {
143161
getRootUri(): string | undefined {
144162
return super.getRootUri();
145163
}
146164
}
165+
kameletDef.spec.template = {
166+
route: {
167+
from: camelFromJson.from,
168+
},
169+
};
147170
const kamelet = new KameletVisualEntityTest(kameletDef);
148171
expect(kamelet.getRootUri()).toBe('timer');
149172
});

packages/ui/src/models/visualization/flows/kamelet-visual-entity.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FromDefinition } from '@kaoto/camel-catalog/types';
1+
import { RouteDefinition } from '@kaoto/camel-catalog/types';
22
import { isDefined } from '@kaoto/forms';
33

44
import { getCamelRandomId } from '../../../camel-utils/camel-random-id';
@@ -15,18 +15,26 @@ import { IClipboardContent } from '../clipboard';
1515
import { AbstractCamelVisualEntity } from './abstract-camel-visual-entity';
1616
import { CamelComponentDefaultService } from './support/camel-component-default.service';
1717

18-
export class KameletVisualEntity extends AbstractCamelVisualEntity<{ id: string; template: { from: FromDefinition } }> {
18+
export class KameletVisualEntity extends AbstractCamelVisualEntity<{
19+
id: string;
20+
template: { route: RouteDefinition };
21+
}> {
1922
id: string;
2023
readonly type = EntityType.Kamelet;
21-
static readonly ROOT_PATH = 'template';
24+
static readonly ROOT_PATH = 'template.route';
2225

2326
constructor(public kamelet: IKameletDefinition) {
27+
const { route, from, ...templateWithoutFrom } = kamelet.spec?.template ?? {};
28+
2429
const spec: IKameletSpec = {
2530
...kamelet.spec,
2631
template: {
27-
...kamelet.spec?.template,
28-
from: {
29-
...kamelet.spec?.template?.from,
32+
...templateWithoutFrom,
33+
route: {
34+
...route,
35+
from: {
36+
...(from ?? route?.from),
37+
},
3038
},
3139
},
3240
definition: {
@@ -35,7 +43,7 @@ export class KameletVisualEntity extends AbstractCamelVisualEntity<{ id: string;
3543
...(kamelet.spec?.definition?.description ? { description: kamelet.spec?.definition?.description } : {}),
3644
},
3745
};
38-
super({ id: kamelet.metadata?.name, template: { from: spec.template.from } });
46+
super({ id: kamelet.metadata?.name, template: { route: spec.template.route } });
3947
this.id = (kamelet?.metadata?.name as string) ?? getCamelRandomId('kamelet');
4048
this.kamelet.metadata = kamelet?.metadata ?? { name: this.id };
4149
this.kamelet.metadata.name = kamelet?.metadata.name ?? this.id;
@@ -71,8 +79,8 @@ export class KameletVisualEntity extends AbstractCamelVisualEntity<{ id: string;
7179
return super.getNodeLabel(path, labelType, ids);
7280
}
7381

74-
toJSON(): { from: FromDefinition } {
75-
return { from: this.entityDef.template.from };
82+
toJSON(): { route: RouteDefinition } {
83+
return { route: this.entityDef.template.route };
7684
}
7785

7886
async fetchNodeSchema(ids: IVisualizationNodeIds): Promise<KaotoSchemaDefinition['schema'] | undefined> {
@@ -130,10 +138,10 @@ export class KameletVisualEntity extends AbstractCamelVisualEntity<{ id: string;
130138
if (
131139
options.mode === AddStepMode.ReplaceStep &&
132140
options.data.path === `${this.getRootPath()}.from` &&
133-
isDefined(this.entityDef.template.from)
141+
isDefined(this.entityDef.template.route.from)
134142
) {
135143
const fromValue = CamelComponentDefaultService.getDefaultFromDefinitionValue(options.definedComponent);
136-
Object.assign(this.entityDef.template.from, fromValue);
144+
Object.assign(this.entityDef.template.route.from, fromValue);
137145
return;
138146
}
139147

@@ -166,7 +174,7 @@ export class KameletVisualEntity extends AbstractCamelVisualEntity<{ id: string;
166174
}
167175

168176
protected getRootUri(): string | undefined {
169-
return this.kamelet.spec.template.from?.uri;
177+
return this.kamelet.spec.template.route.from.uri;
170178
}
171179

172180
private async getRootKameletSchema(): Promise<KaotoSchemaDefinition['schema']> {

packages/ui/src/services/parsers/kamelet-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class KameletParser {
99
static parseKameletEntity(entity: KameletVisualEntity): ParsedTable[] {
1010
const answer: ParsedTable[] = [];
1111

12-
const routeTable = KameletParser.parseRoute(entity.kamelet.spec.template.from);
12+
const routeTable = KameletParser.parseRoute(entity.kamelet.spec.template.route.from);
1313
answer.push(routeTable);
1414

1515
const definitionTable = KameletParser.parseDefinition(entity.kamelet.spec.definition);

0 commit comments

Comments
 (0)