From 7eedcb85862878b51c6ad1b3d81061cd71f68537 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Thu, 13 Aug 2026 21:21:20 -0300 Subject: [PATCH] fix: decode node IDs as string in ClientCallable methods Close #102 --- .../vaadin/addons/orgchart/OrgChart.java | 50 ++++++++++++++++++- .../META-INF/frontend/fc-orgchart.js | 4 +- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/orgchart/OrgChart.java b/src/main/java/com/flowingcode/vaadin/addons/orgchart/OrgChart.java index 7b19235..e231a0f 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/orgchart/OrgChart.java +++ b/src/main/java/com/flowingcode/vaadin/addons/orgchart/OrgChart.java @@ -383,6 +383,28 @@ private List convertJsonArrayToIntegerList(JsonArray jsonIds) { return idList; } + /** + * Converts a comma-separated string of numeric IDs to a JsonArray. + *

+ * IDs are received from the client as a string because {@code String} is the only argument type + * that every supported Vaadin version is able to decode for a {@link ClientCallable} method: up + * to Vaadin 24 the RPC decoder only accepts {@code String}, {@code Boolean}, {@code Integer}, + * {@code Double} and {@code elemental.json.JsonValue}, while Vaadin 25 decodes with Jackson and + * no longer knows about {@code elemental.json}. + * + * @param ids comma-separated numeric IDs, possibly empty or {@code null} + * @return the IDs as a JsonArray of numbers + */ + private JsonArray convertIdsToJsonArray(String ids) { + JsonArray jsonIds = Json.createArray(); + if (ids != null && !ids.isEmpty()) { + for (String id : ids.split(",")) { + jsonIds.set(jsonIds.length(), Double.parseDouble(id.trim())); + } + } + return jsonIds; + } + /** * Appends a list of items to a parent node's children list. * @@ -436,6 +458,20 @@ public void addSiblings(Integer nodeId, List siblings) { } + /** + * Bridge for {@link #onSiblingsAdded(String, JsonArray)}, invoked from the client side. The IDs + * are received as a comma-separated string, since {@code JsonArray} arguments cannot be decoded + * by all supported Vaadin versions. + * + * @param nodeId the ID of the node that received new siblings + * @param siblingIds comma-separated IDs for the newly added siblings + * @see #convertIdsToJsonArray(String) + */ + @ClientCallable + private void onSiblingsAdded(String nodeId, String siblingIds) { + onSiblingsAdded(nodeId, convertIdsToJsonArray(siblingIds)); + } + /** * Handles sibling addition events from the client side. Converts the received JsonArray of * sibling IDs to a List and fires a {@link SiblingsAddedEvent}. @@ -443,7 +479,6 @@ public void addSiblings(Integer nodeId, List siblings) { * @param nodeId the ID of the node that received new siblings * @param siblingIds array of IDs for the newly added siblings */ - @ClientCallable private void onSiblingsAdded(String nodeId, JsonArray siblingIds) { // Find the node where siblings were added OrgChartItem targetItem = getById(Integer.valueOf(nodeId), orgChartItem); @@ -518,7 +553,20 @@ public void addChildren(Integer nodeId, List children) { } } + /** + * Bridge for {@link #onChildrenAdded(String, JsonArray)}, invoked from the client side. The IDs + * are received as a comma-separated string, since {@code JsonArray} arguments cannot be decoded + * by all supported Vaadin versions. + * + * @param nodeId the ID of the parent node that received new children + * @param childIds comma-separated IDs for the newly added children + * @see #convertIdsToJsonArray(String) + */ @ClientCallable + private void onChildrenAdded(String nodeId, String childIds) { + onChildrenAdded(nodeId, convertIdsToJsonArray(childIds)); + } + private void onChildrenAdded(String nodeId, JsonArray childIds) { // Find the parent node where children were added OrgChartItem parentItem = getById(Integer.valueOf(nodeId), orgChartItem); diff --git a/src/main/resources/META-INF/frontend/fc-orgchart.js b/src/main/resources/META-INF/frontend/fc-orgchart.js index ab687fd..4a518b5 100644 --- a/src/main/resources/META-INF/frontend/fc-orgchart.js +++ b/src/main/resources/META-INF/frontend/fc-orgchart.js @@ -169,7 +169,7 @@ class FCOrgChart extends PolymerElement { // Notify server about siblings added with just the IDs const siblingIds = siblingsData.map(sibling => sibling.id); - this.$server.onSiblingsAdded(nodeId, siblingIds); + this.$server.onSiblingsAdded(nodeId, siblingIds.join(',')); } } } @@ -190,7 +190,7 @@ class FCOrgChart extends PolymerElement { this._chartInstance.addChildren($node, childrenData); // Notify server about children added with just the IDs const childIds = childrenData.map(child => child.id); - this.$server.onChildrenAdded(nodeId, childIds); + this.$server.onChildrenAdded(nodeId, childIds.join(',')); } } }