davsclaus commented on code in PR #27699:
URL: https://github.com/apache/camel/pull/27699#discussion_r4245286144


##########
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:
   Good point: each occurrence is now compared with the one before it, so a key 
given three times gives two messages (lines 10 and 20, then 20 and 30) and 
every lost one is named. Test `aKeyGivenThreeTimesNamesBothLostOnes` 
(6d9b4710522c).



##########
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:
   Yes, on purpose: the Jackson tree already dropped the first of two equal 
keys, so the YAML nodes are the only place to see them, and `describeAll` 
parses the same nodes for the line of every error anyway. Route files are 
small; left as is.



-- 
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