This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new ffe3e40d72bd CAMEL-25238: a chosen branch silences the other branches
of a pick-one construct (#27231)
ffe3e40d72bd is described below
commit ffe3e40d72bd6271b1ba894da18697ee9ea1d89d
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Oct 1 22:35:51 2026 +0200
CAMEL-25238: a chosen branch silences the other branches of a pick-one
construct (#27231)
* 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-25238: review - Javadoc back on isRuntimeAcceptedScalar, unused
requiredPropertyOf removed
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---------
Signed-off-by: Claus Ibsen <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
---
.../camel/dsl/yaml/validator/YamlValidator.java | 52 ++++++++-
.../dsl/yaml/validator/OneOfAlternativesTest.java | 116 +++++++++++++++++++++
2 files changed, 167 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..876015336b5a 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
@@ -1209,6 +1213,52 @@ public class YamlValidator {
return groups;
}
+ /**
+ * 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, they name the keys the file did not write;
if the object at that place has anything
+ * in it, a branch was chosen and the rest are noise. When it is empty,
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);
+ }
+ }
+ }
+
+ /**
+ * 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);
+ }
+
/**
* Whether the schema rejected a scalar that the Camel runtime accepts, in
which case the error is dropped.
* <p>
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'"));
+ }
+}