From 5f64111275922295cfe3a82792c69cd0deffee64 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Mon, 31 Aug 2026 20:47:54 +0530 Subject: [PATCH] Fix for uploading issue --- .../helper/AttachmentsHandlerUtils.java | 61 +++++++++++++++---- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java index f5bc8ee9..b9f71050 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java @@ -194,7 +194,7 @@ private static void processAttachmentPaths( List attachmentPaths) { for (String attachmentPath : attachmentPaths) { String entityPath = buildEntityPath(entity, targetEntity, attachmentPath); - String actualPath = buildActualPath(entity, compositionName, attachmentPath); + String actualPath = buildActualPath(entity, compositionName, targetEntity, attachmentPath); // Only add the mapping if both paths are non-null and the key doesn't already exist // This preserves direct attachment mappings from being overwritten by nested ones @@ -272,8 +272,9 @@ private static String buildEntityPath( // Direct attachment: use parent entity path entityPath = parentEntity.getQualifiedName() + "." + attachmentPart; } else { - // Nested attachment: use target entity path to ensure uniqueness - entityPath = targetEntity.getQualifiedName() + "." + attachmentPart; + // Nested attachment: use attachmentPath as-is — it already encodes the owning entity + // (e.g. "AdminService.Sections.attachments"), ensuring uniqueness at any depth. + entityPath = attachmentPath; } return entityPath; } @@ -284,19 +285,34 @@ private static String buildEntityPath( } private static String buildActualPath( - CdsEntity parentEntity, String compositionPropertyName, String attachmentPath) { + CdsEntity parentEntity, + String compositionPropertyName, + CdsEntity targetEntity, + String attachmentPath) { try { String[] pathParts = attachmentPath.split("\\."); if (pathParts.length >= 3) { - // Get the attachment part (last part) String attachmentPart = pathParts[pathParts.length - 1]; - - // Build the new path using parent entity qualified name + composition property name - return parentEntity.getQualifiedName() - + "." - + compositionPropertyName - + "." - + attachmentPart; + String ownerEntityQN = attachmentPath.substring(0, attachmentPath.lastIndexOf('.')); + + if (ownerEntityQN.equals(targetEntity.getQualifiedName())) { + return parentEntity.getQualifiedName() + + "." + + compositionPropertyName + + "." + + attachmentPart; + } else { + String intermediatePath = findPathToEntity(targetEntity, ownerEntityQN, new HashSet<>()); + if (intermediatePath != null) { + return parentEntity.getQualifiedName() + + "." + + compositionPropertyName + + "." + + intermediatePath + + "." + + attachmentPart; + } + } } } catch (Exception e) { logger.warn(SDMUtils.getErrorMessage("FETCH_ATTACHMENT_COMPOSITION_ERROR"), e.getMessage()); @@ -304,6 +320,27 @@ private static String buildActualPath( return null; } + private static String findPathToEntity( + CdsEntity fromEntity, String toEntityQN, Set visited) { + if (visited.contains(fromEntity.getQualifiedName())) return null; + visited.add(fromEntity.getQualifiedName()); + List comps = fromEntity.compositions().collect(java.util.stream.Collectors.toList()); + for (Object comp : comps) { + com.sap.cds.reflect.CdsElement element = (com.sap.cds.reflect.CdsElement) comp; + if (!element.getType().isAssociation()) continue; + CdsAssociationType assocType = (CdsAssociationType) element.getType(); + CdsEntity target = assocType.getTarget(); + if (toEntityQN.equals(target.getQualifiedName())) { + return element.getName(); + } + String subPath = findPathToEntity(target, toEntityQN, visited); + if (subPath != null) { + return element.getName() + "." + subPath; + } + } + return null; + } + private static List> findNestedAttachments( Map entity, String attachmentKey, String parentKey, String currentParentKey) { List> result = new ArrayList<>();