Skip to content

Commit a3f1801

Browse files
javier-godoypaodb
authored andcommitted
fix: decode node IDs as string in ClientCallable methods
Close #102
1 parent 4ee2442 commit a3f1801

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎src/main/java/com/flowingcode/vaadin/addons/orgchart/OrgChart.java‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,28 @@ private List<Integer> convertJsonArrayToIntegerList(JsonArray jsonIds) {
383383
return idList;
384384
}
385385

386+
/**
387+
* Converts a comma-separated string of numeric IDs to a JsonArray.
388+
* <p>
389+
* IDs are received from the client as a string because {@code String} is the only argument type
390+
* that every supported Vaadin version is able to decode for a {@link ClientCallable} method: up
391+
* to Vaadin 24 the RPC decoder only accepts {@code String}, {@code Boolean}, {@code Integer},
392+
* {@code Double} and {@code elemental.json.JsonValue}, while Vaadin 25 decodes with Jackson and
393+
* no longer knows about {@code elemental.json}.
394+
*
395+
* @param ids comma-separated numeric IDs, possibly empty or {@code null}
396+
* @return the IDs as a JsonArray of numbers
397+
*/
398+
private JsonArray convertIdsToJsonArray(String ids) {
399+
JsonArray jsonIds = Json.createArray();
400+
if (ids != null && !ids.isEmpty()) {
401+
for (String id : ids.split(",")) {
402+
jsonIds.set(jsonIds.length(), Double.parseDouble(id.trim()));
403+
}
404+
}
405+
return jsonIds;
406+
}
407+
386408
/**
387409
* Appends a list of items to a parent node's children list.
388410
*
@@ -436,14 +458,27 @@ public void addSiblings(Integer nodeId, List<OrgChartItem> siblings) {
436458
}
437459

438460

461+
/**
462+
* Bridge for {@link #onSiblingsAdded(String, JsonArray)}, invoked from the client side. The IDs
463+
* are received as a comma-separated string, since {@code JsonArray} arguments cannot be decoded
464+
* by all supported Vaadin versions.
465+
*
466+
* @param nodeId the ID of the node that received new siblings
467+
* @param siblingIds comma-separated IDs for the newly added siblings
468+
* @see #convertIdsToJsonArray(String)
469+
*/
470+
@ClientCallable
471+
private void onSiblingsAdded(String nodeId, String siblingIds) {
472+
onSiblingsAdded(nodeId, convertIdsToJsonArray(siblingIds));
473+
}
474+
439475
/**
440476
* Handles sibling addition events from the client side. Converts the received JsonArray of
441477
* sibling IDs to a List and fires a {@link SiblingsAddedEvent}.
442478
*
443479
* @param nodeId the ID of the node that received new siblings
444480
* @param siblingIds array of IDs for the newly added siblings
445481
*/
446-
@ClientCallable
447482
private void onSiblingsAdded(String nodeId, JsonArray siblingIds) {
448483
// Find the node where siblings were added
449484
OrgChartItem targetItem = getById(Integer.valueOf(nodeId), orgChartItem);
@@ -518,7 +553,20 @@ public void addChildren(Integer nodeId, List<OrgChartItem> children) {
518553
}
519554
}
520555

556+
/**
557+
* Bridge for {@link #onChildrenAdded(String, JsonArray)}, invoked from the client side. The IDs
558+
* are received as a comma-separated string, since {@code JsonArray} arguments cannot be decoded
559+
* by all supported Vaadin versions.
560+
*
561+
* @param nodeId the ID of the parent node that received new children
562+
* @param childIds comma-separated IDs for the newly added children
563+
* @see #convertIdsToJsonArray(String)
564+
*/
521565
@ClientCallable
566+
private void onChildrenAdded(String nodeId, String childIds) {
567+
onChildrenAdded(nodeId, convertIdsToJsonArray(childIds));
568+
}
569+
522570
private void onChildrenAdded(String nodeId, JsonArray childIds) {
523571
// Find the parent node where children were added
524572
OrgChartItem parentItem = getById(Integer.valueOf(nodeId), orgChartItem);

‎src/main/resources/META-INF/frontend/fc-orgchart.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class FCOrgChart extends PolymerElement {
169169

170170
// Notify server about siblings added with just the IDs
171171
const siblingIds = siblingsData.map(sibling => sibling.id);
172-
this.$server.onSiblingsAdded(nodeId, siblingIds);
172+
this.$server.onSiblingsAdded(nodeId, siblingIds.join(','));
173173
}
174174
}
175175
}
@@ -190,7 +190,7 @@ class FCOrgChart extends PolymerElement {
190190
this._chartInstance.addChildren($node, childrenData);
191191
// Notify server about children added with just the IDs
192192
const childIds = childrenData.map(child => child.id);
193-
this.$server.onChildrenAdded(nodeId, childIds);
193+
this.$server.onChildrenAdded(nodeId, childIds.join(','));
194194
}
195195
}
196196
}

0 commit comments

Comments
 (0)