gnodet-bot commented on code in PR #27699:
URL: https://github.com/apache/camel/pull/27699#discussion_r4245302828
##########
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:
💡 **Minor: unconditional second parse on every validation**
`YamlPointerLines.root(content)` (snakeyaml-engine) is called
unconditionally on line 203, before checking whether `duplicates` is empty. On
the happy path — no duplicate keys — the YAML is parsed twice for no benefit.
```suggestion
List<String> duplicates = errors.isEmpty()
? List.of()
: DuplicateKeys.find(YamlPointerLines.root(content));
```
…or, alternatively, move the `find` call inside a lazy supplier / guard. The
common case (no duplicates) pays zero extra cost.
##########
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:
💡 **Minor: implicit null-safety in `find()`**
`YamlPointerLines.root()` can return `null` (lines 47 and 54 of
`YamlPointerLines.java`). When it does, `walk(null, null, answer)` is called,
and the `instanceof` checks silently skip it — no crash, just an empty list
returned. This works today, but it relies on Java’s `null instanceof T` being
`false`. A one-line explicit guard makes the contract visible and protects
against future refactors:
```suggestion
static List<String> find(Node root) {
if (root == null) {
return List.of();
}
List<String> answer = new ArrayList<>();
walk(root, null, answer);
return answer;
}
```
--
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]