gnodet-bot commented on code in PR #27699:
URL: https://github.com/apache/camel/pull/27699#discussion_r4245272356


##########
dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/DuplicateKeys.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.yaml.validator;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.snakeyaml.engine.v2.nodes.MappingNode;
+import org.snakeyaml.engine.v2.nodes.Node;
+import org.snakeyaml.engine.v2.nodes.NodeTuple;
+import org.snakeyaml.engine.v2.nodes.ScalarNode;
+import org.snakeyaml.engine.v2.nodes.SequenceNode;
+
+/**
+ * CAMEL-25560: a key given twice in one mapping. YAML keeps only the last of 
the two (the tree the validator reads
+ * does, and the YAML DSL when it loads the route), so the first is lost 
without a word: an edit that adds a message:
+ * under a log: and leaves the old one runs with the old one. The YAML nodes 
still have both, with their lines.
+ */
+final class DuplicateKeys {
+
+    private DuplicateKeys() {
+    }
+
+    /** One message for each key that repeats a key of its mapping, in the 
order of the file; empty for no YAML. */
+    static List<String> find(Node root) {
+        List<String> answer = new ArrayList<>();
+        walk(root, null, answer);
+        return answer;
+    }
+
+    private static void walk(Node node, String owner, List<String> answer) {
+        if (node instanceof SequenceNode seq) {
+            for (Node item : seq.getValue()) {
+                walk(item, owner, answer);
+            }
+        } else if (node instanceof MappingNode map) {
+            Map<String, NodeTuple> seen = new HashMap<>();
+            for (NodeTuple t : map.getValue()) {
+                String key = t.getKeyNode() instanceof ScalarNode s ? 
s.getValue() : null;
+                // <<: is a merge key, which YAML allows more than once
+                if (key != null && !"<<".equals(key)) {
+                    NodeTuple first = seen.putIfAbsent(key, t);
+                    if (first != null) {
+                        answer.add(message(key, owner, first, t));
+                    }
+                }
+                walk(t.getValueNode(), key != null ? key : owner, answer);
+            }
+        }
+    }
+
+    private static String message(String key, String owner, NodeTuple first, 
NodeTuple again) {
+        int before = line(first.getKeyNode());
+        int line = line(again.getKeyNode());
+        String where = owner != null ? "this " + owner + ":" : "one mapping";
+        String lost = "line " + line + ": " + key + ": is given twice in " + 
where + " (lines " + before + " and " + line
+                      + "); YAML keeps only the last one, so the " + key + ": 
at line " + before + " is lost";

Review Comment:
   💡 **Nit:** With 3+ identical keys (e.g. lines 10, 20, 30), `putIfAbsent` 
keeps `first` pointing to line 10 throughout. The message says `"the key at 
line 10 is lost"` — correct, but the occurrence at line 20 is also silently 
discarded. For the common 2-key case this is perfect; you may want to add a 
note when `before` is not the direct predecessor of `line` to avoid user 
confusion.
   
   Not blocking — the current message is accurate for the overwhelmingly common 
2-occurrence case.



##########
dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/YamlValidator.java:
##########
@@ -198,7 +198,16 @@ public List<Error> validate(String content, Set<String> 
bodylessEndpoints) throw
         }
         try {
             var target = mapper.readTree(content);
-            return validate(target, bodylessEndpoints);
+            List<Error> errors = validate(target, bodylessEndpoints);
+            // CAMEL-25560: the tree keeps the last of two equal keys, so a 
key given twice is only seen in the YAML nodes
+            List<String> duplicates = 
DuplicateKeys.find(YamlPointerLines.root(content));

Review Comment:
   💡 **Observation:** `content` is parsed twice here — once by 
`mapper.readTree(content)` (Jackson/line 200) for schema validation, and again 
by `YamlPointerLines.root(content)` (snakeyaml-engine) for duplicate-key 
detection. Both parsers are fast for typical YAML route files so it's fine in 
practice. Worth a comment if this becomes a performance concern on large files.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to