This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-25238 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 433e32f6bada140ee5aad4ff000488d8366c97de Author: Claus Ibsen <[email protected]> AuthorDate: Thu Oct 1 19:23:57 2026 +0200 CAMEL-25238: a chosen branch silences the other branches of a pick-one construct The branches of a oneOf such as unmarshal each require their own key, so a file that chose one collects "required property X not found" for every key it did not write. The key it did write is missing from that list, because its branch got past the required check and failed, if at all, further in. filterOneOfNoise then keeps one branch for all the places validated against the same construct, so a second place inherits the first one's winner and is told it needs a key it does not have and does not need: - choice: when: - simple: "${header.encoding} == 'JSON'" steps: [ { unmarshal: { fhirJson: { fhirVersion: "{{fhirVersion}}" } } } ] - simple: "${header.encoding} == 'XML'" steps: [ { unmarshal: { fhirXml: { fhirVersion: "{{fhirVersion}}" } } } ] Line 16: /0/.../when/1/steps/0/unmarshal: required property 'fhirJson' not found The file is right, and fhir-sink.kamelet.yaml of camel-kamelets has this shape. The required errors of a pick-one construct are now dropped where the object at that place has something in it: one of the branches was chosen, so the others only say which keys were not used. An empty object, or a key no branch knows, still gets the construct's own error and the unknown-property error, which are what tell the author what to do. Verified on the kamelets of camel-kamelets: all 250 validate clean, where before fhir-sink did not. The documentation corpus and the XML round-trip corpus of this module are unchanged, including testTypeMismatchFiltersOneOfNoise, which is the assertion that a more general fix of the same bug could not keep. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Bp3538HRBPMQkb5ta9xRaj --- .../camel/dsl/yaml/validator/YamlValidator.java | 60 ++++++++++- .../dsl/yaml/validator/OneOfAlternativesTest.java | 116 +++++++++++++++++++++ 2 files changed, 175 insertions(+), 1 deletion(-) diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/YamlValidator.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/YamlValidator.java index 688fce7f3234..394d0ab6151d 100644 --- a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/YamlValidator.java +++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/YamlValidator.java @@ -665,7 +665,11 @@ public class YamlValidator { private List<Error> validate(JsonNode target, Set<String> bodylessEndpoints) { Schema against = isKamelet(target) ? kameletSchema : schema; - var errors = filterOneOfNoise(new ArrayList<>(against.validate(target))); + // before the noise filter, which keeps one branch per construct: the alternatives of a construct are the + // keys its branches require, and they are only all present while nothing has been dropped yet + List<Error> raw = new ArrayList<>(against.validate(target)); + removeSatisfiedAlternatives(raw); + var errors = filterOneOfNoise(raw); // the checks below walk the document looking for Camel nodes, and spec.definition of a Kamelet is a JSON // schema of its properties, not Camel: a property named delay is not the Delay EIP, which is what // aws-s3-source and eight others were told. Removing the branch leaves every path under spec.template as it @@ -1231,6 +1235,60 @@ public class YamlValidator { * {@code BeanConstructorDefinition.index}, which is a map key and is not reachable from the schema - see * CAMEL-24696 before exposing it. */ + /** + * Drops "required property X not found" where one of the alternatives is in fact there. + * <p/> + * The branches of a {@code oneOf} each require their own key, and when all of them are reported the file is told it + * needs a key it does not: {@code unmarshal: {fhirXml: ...}} in one when branch of a choice was told "required + * property 'fhirJson' not found", because the winner of the other when branch was chosen for it as well. Collected + * per construct and per place, the required names of the branches are the alternatives; if the object at that place + * has any of them, one branch is satisfied and the rest are noise. When none of them is there, the file really does + * have to pick one and the errors stay (CAMEL-25238). + */ + private static void removeSatisfiedAlternatives(List<Error> errors) { + Map<String, List<Error>> groups = new LinkedHashMap<>(); + for (Error e : errors) { + if (!"required".equals(e.getKeyword())) { + continue; + } + String construct = constructOf(String.valueOf(e.getEvaluationPath())); + if (construct != null) { + groups.computeIfAbsent(construct + "@" + e.getInstanceLocation(), k -> new ArrayList<>()).add(e); + } + } + for (List<Error> group : groups.values()) { + JsonNode instance = group.get(0).getInstanceNode(); + if (instance == null || !instance.isObject()) { + continue; + } + // the branches of a pick-one construct each require their own key, so one error per key the file did not + // write. The key it did write is the one missing from them: that branch got past its required check and + // failed, if at all, further in. So a non-empty object means a branch was chosen and the rest are noise. + // An object with nothing in it, or with a key no branch knows, still gets the construct's own error and + // the additionalProperties error, which is what actually tells the author what to do. + if (!instance.isEmpty() && group.size() > 1) { + errors.removeAll(group); + } + } + } + + private static final Pattern REQUIRED_PROPERTY = Pattern.compile("required property '([^']+)' not found"); + + /** The property name a "required property 'X' not found" error names, or null. */ + private static String requiredPropertyOf(Error error) { + Matcher m = REQUIRED_PROPERTY.matcher(String.valueOf(error.getMessage())); + return m.find() ? m.group(1) : null; + } + + /** + * The schema construct an error came from when it came from inside a branch of a {@code oneOf} or an {@code anyOf}: + * its evaluation path up to and including that keyword, or null when it is not inside one. + */ + private static String constructOf(String evaluationPath) { + int at = Math.max(evaluationPath.lastIndexOf("/oneOf/"), evaluationPath.lastIndexOf("/anyOf/")); + return at < 0 ? null : evaluationPath.substring(0, at + 7); + } + static boolean isRuntimeAcceptedScalar(Error error) { String keyword = error.getKeyword(); if (!"type".equals(keyword) && !"enum".equals(keyword)) { diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/OneOfAlternativesTest.java b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/OneOfAlternativesTest.java new file mode 100644 index 000000000000..0004656cc589 --- /dev/null +++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/OneOfAlternativesTest.java @@ -0,0 +1,116 @@ +/* + * 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.List; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * CAMEL-25238: the branches of a pick-one construct each require their own key, so a file that chose one of them + * collects an error for every key it did not write. The one it did write is missing from them, because that branch got + * past its required check, so the file is told it needs a key it does not. + */ +class OneOfAlternativesTest { + + private static List<String> validate(String content) throws Exception { + YamlValidator v = new YamlValidator(); + return YamlValidator.describeAll(content, v.validate(content)); + } + + @Test + void twoBranchesUnmarshallingWithDifferentDataFormats() throws Exception { + // the shape of fhir-sink.kamelet.yaml: each when branch unmarshals with a different fhir data format, and the + // second was told "required property 'fhirJson' not found" + assertThat(validate(""" + - route: + id: fhir + from: + uri: timer:tick + steps: + - choice: + when: + - simple: "${header.encoding} == 'JSON'" + steps: + - unmarshal: + fhirJson: + fhirVersion: "{{fhirVersion}}" + - simple: "${header.encoding} == 'XML'" + steps: + - unmarshal: + fhirXml: + fhirVersion: "{{fhirVersion}}" + """)).isEmpty(); + } + + @Test + void oneBranchChosenInSeveralPlacesIsStillFine() throws Exception { + assertThat(validate(""" + - route: + id: two + from: + uri: timer:tick + steps: + - unmarshal: + json: + library: Jackson + - marshal: + csv: {} + """)).isEmpty(); + } + + @Test + void choosingNothingIsStillReported() throws Exception { + // the construct's own error says the file has to pick one, and that must survive + assertThat(validate(""" + - route: + id: none + from: + uri: timer:tick + steps: + - unmarshal: {} + """)).isNotEmpty(); + } + + @Test + void choosingSomethingThatIsNotADataFormatIsStillReported() throws Exception { + assertThat(validate(""" + - route: + id: bogus + from: + uri: timer:tick + steps: + - unmarshal: + notADataFormat: {} + """)).isNotEmpty(); + } + + @Test + void anUnrelatedMistakeIsUntouched() throws Exception { + assertThat(validate(""" + - route: + id: typo + from: + uri: timer:tick + steps: + - log: + mesage: "typo" + """)).anyMatch(e -> e.contains("did you mean 'message'")); + } +}
