Skip to content

Commit

Permalink
Fixing adding nested objects if path is like source.id
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Dec 24, 2024
1 parent 150c971 commit 3c18402
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -137,6 +138,7 @@ public static void substituteValueInPayload(MappingType type, MappingSubstitutio
// TOFDO fix this, we have to differentiate between {"nullField": null } and
// "nonExisting"
try {
if (sub == null) return;
if ("$".equals(keys)) {
Object replacement = sub;
if (replacement instanceof Map<?, ?> map) {
Expand All @@ -150,7 +152,8 @@ public static void substituteValueInPayload(MappingType type, MappingSubstitutio
if ((sub.repairStrategy.equals(RepairStrategy.REMOVE_IF_MISSING_OR_NULL) && subValueMissingOrNull)) {
jsonObject.delete(keys);
} else if (sub.repairStrategy.equals(RepairStrategy.CREATE_IF_MISSING)) {
jsonObject.set("$." + keys, sub.value);
// jsonObject.put("$", keys, sub.value);
addNestedValue(jsonObject, keys, sub.value);
} else {
jsonObject.set(keys, sub.value);
}
Expand All @@ -160,11 +163,30 @@ public static void substituteValueInPayload(MappingType type, MappingSubstitutio
}
}

public static void processSubstitute(String tenant, List<MappingSubstitution.SubstituteValue> postProcessingCacheEntry,
public static void addNestedValue(DocumentContext jsonObject, String path, Object value) {
String[] parts = path.split("\\.");
StringBuilder currentPath = new StringBuilder("$");

// Create all parent objects except the last one
for (int i = 0; i < parts.length - 1; i++) {
if (i == 0) {
jsonObject.put("$", parts[i], new HashMap<>());
} else {
jsonObject.put(currentPath.toString(), parts[i], new HashMap<>());
}
currentPath.append(".").append(parts[i]);
}

// Add the final value
jsonObject.put(currentPath.toString(), parts[parts.length - 1], value);
}

public static void processSubstitute(String tenant,
List<MappingSubstitution.SubstituteValue> postProcessingCacheEntry,
Object extractedSourceContent, MappingSubstitution substitution, Mapping mapping) {
if (extractedSourceContent == null) {
log.warn("Tenant {} - Substitution {} not in message payload. Check your mapping {}", tenant,
substitution.pathSource, mapping.getMappingTopic());
substitution.pathSource, mapping.getMappingTopic());
postProcessingCacheEntry
.add(new MappingSubstitution.SubstituteValue(extractedSourceContent,
MappingSubstitution.SubstituteValue.TYPE.IGNORE, substitution.repairStrategy));
Expand All @@ -176,7 +198,7 @@ public static void processSubstitute(String tenant, List<MappingSubstitution.Sub
postProcessingCacheEntry
.add(new MappingSubstitution.SubstituteValue(extractedSourceContent,
MappingSubstitution.SubstituteValue.TYPE.NUMBER, substitution.repairStrategy));
} else if (isArray(extractedSourceContent)) {
} else if (isArray(extractedSourceContent)) {
postProcessingCacheEntry
.add(new MappingSubstitution.SubstituteValue(extractedSourceContent,
MappingSubstitution.SubstituteValue.TYPE.ARRAY, substitution.repairStrategy));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ private ProcessingContext<T> getBuildProcessingContext(ProcessingContext<T> cont
if (pathsTargetForDeviceIdentifiers.contains(pathTarget) && mapping.useExternalId) {
ExternalIDRepresentation sourceId = c8yAgent.resolveExternalId2GlobalId(tenant,
new ID(mapping.externalIdType, substitute.value.toString()), context);
// since the attributes identifying the MEA and Inventory requests are removed
// during the design time, they have to be added before sending
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
if (sourceId == null && mapping.createNonExistingDevice) {
ManagedObjectRepresentation attocDevice = null;
Map<String, Object> request = new HashMap<String, Object>();
Expand Down Expand Up @@ -210,12 +213,7 @@ private ProcessingContext<T> getBuildProcessingContext(ProcessingContext<T> cont
substitute.value = null;
} else {
substitute.value = sourceId.getManagedObject().getId().getValue();
}
// since the attributes identifying the MEA and Inventory requests are removed
// during the design time, they have to be added before sending
if (mapping.getGenericDeviceIdentifier().equals(pathTarget)) {
substitute.repairStrategy = RepairStrategy.CREATE_IF_MISSING;
}
}
}
substituteValueInPayload(mapping.mappingType, substitute, payloadTarget,
mapping.transformGenericPath2C8YPath(pathTarget));
Expand Down

0 comments on commit 3c18402

Please sign in to comment.