davsclaus commented on code in PR #27699:
URL: https://github.com/apache/camel/pull/27699#discussion_r4245313276
##########
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:
Not this one: a file with a key given twice usually passes the schema (the
Jackson tree only has the last value, which is valid), so with
`errors.isEmpty() ? List.of()` the duplicates would go unreported in exactly
the case they matter; every test of this PR is such a file. The second parse is
of a small route file; left as is.
##########
dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/DuplicateKeys.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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;
Review Comment:
`find(null)` returning an empty list is the documented contract ("empty for
no YAML"), and `walk` takes null on purpose for the same reason; 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]