diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java index a56a7d2a..b1f79db0 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java @@ -53,7 +53,7 @@ public SDMCreateAttachmentsHandler( @Before @HandlerOrder(HandlerOrder.EARLY) public void processBefore(CdsCreateEventContext context, List data) throws IOException { - // Get comprehensive attachment composition details for each entity + logger.info("Target Entity : " + context.getTarget().getQualifiedName()); for (CdsData entityData : data) { Map> attachmentCompositionDetails = AttachmentsHandlerUtils.getAttachmentCompositionDetails( 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 5612d0ea..a55a3e5f 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 @@ -102,9 +102,7 @@ private static void processDirectAttachmentComposition( : null; if (isDirectAttachmentTargetAspect(targetAspect)) { - String serviceName = entity.getQualifiedName().split("\\.")[0]; - String entityName = entity.getName(); - String directPath = serviceName + "." + entityName + "." + compositionName; + String directPath = entity.getQualifiedName() + "." + compositionName; pathMapping.put(directPath, directPath); } } @@ -165,7 +163,9 @@ private static void processAttachmentPaths( String entityPath = buildEntityPath(entity, targetEntity, attachmentPath); String actualPath = buildActualPath(entity, compositionName, attachmentPath); - if (entityPath != null && actualPath != null) { + // 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 + if (entityPath != null && actualPath != null && !pathMapping.containsKey(entityPath)) { pathMapping.put(entityPath, actualPath); } } @@ -219,17 +219,20 @@ private static String buildEntityPath( try { String[] pathParts = attachmentPath.split("\\."); if (pathParts.length >= 3) { - // Get the service name (first part) - String serviceName = pathParts[0]; - - // Get the target entity name (without service prefix) - String targetEntityName = targetEntity.getName(); - // Get the attachment part (last part) String attachmentPart = pathParts[pathParts.length - 1]; - // Build the entity path: ServiceName.EntityName.attachments - return serviceName + "." + targetEntityName + "." + attachmentPart; + // For nested compositions, use the full target entity path to ensure uniqueness + // For direct attachments on the parent entity, the targetEntity equals parentEntity + String entityPath; + if (targetEntity.getQualifiedName().equals(parentEntity.getQualifiedName())) { + // Direct attachment: use parent entity path + entityPath = parentEntity.getQualifiedName() + "." + attachmentPart; + } else { + // Nested attachment: use target entity path to ensure uniqueness + entityPath = targetEntity.getQualifiedName() + "." + attachmentPart; + } + return entityPath; } } catch (Exception e) { logger.warn(SDMConstants.FETCH_ATTACHMENT_COMPOSITION_ERROR, e.getMessage()); @@ -242,15 +245,15 @@ private static String buildActualPath( try { String[] pathParts = attachmentPath.split("\\."); if (pathParts.length >= 3) { - // Get the service name (first part) - String serviceName = pathParts[0]; - - // Replace the entity name with the composition property name - // Keep the attachment part (last part) + // Get the attachment part (last part) String attachmentPart = pathParts[pathParts.length - 1]; - // Build the new path: ServiceName.compositionPropertyName.attachments - return serviceName + "." + compositionPropertyName + "." + attachmentPart; + // Build the new path using parent entity qualified name + composition property name + return parentEntity.getQualifiedName() + + "." + + compositionPropertyName + + "." + + attachmentPart; } } catch (Exception e) { logger.warn(SDMConstants.FETCH_ATTACHMENT_COMPOSITION_ERROR, e.getMessage());