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 e1ff609bc5d4 CAMEL-22931: A camel specific yaml dsl parser that can
parse and load yaml sources to check if they are loadable by Camel which is a
faster and lighter than the yaml schema validator which is heavy and memory
hungry due to our very big and complex schema file.
e1ff609bc5d4 is described below
commit e1ff609bc5d4377d26b87fb7ddf79459a9c452e2
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Feb 11 21:29:50 2026 +0100
CAMEL-22931: A camel specific yaml dsl parser that can parse and load yaml
sources to check if they are loadable by Camel which is a faster and lighter
than the yaml schema validator which is heavy and memory hungry due to our very
big and complex schema file.
---
.../org/apache/camel/support/DefaultRegistry.java | 2 +-
.../camel-yaml-dsl-validator/pom.xml | 12 +++
.../camel/dsl/yaml/validator/CamelYamlParser.java | 86 ++++++++++++++++++++++
.../yaml/validator/stub/StubBeanRepository.java | 83 +++++++++++++++++++++
.../dsl/yaml/validator/stub/StubDataFormat.java | 48 ++++++++++++
.../dsl/yaml/validator/stub/StubLanguage.java | 40 ++++++++++
.../dsl/yaml/validator/stub/StubTransformer.java | 32 ++++++++
.../dsl/yaml/validator/CamelYamlParserTest.java | 47 ++++++++++++
8 files changed, 349 insertions(+), 1 deletion(-)
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
index fb9b3f697129..e691ac749a09 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultRegistry.java
@@ -174,7 +174,7 @@ public class DefaultRegistry extends ServiceSupport
implements Registry, LocalBe
* Adds a custom {@link BeanRepository}.
*/
public void addBeanRepository(BeanRepository repository) {
- if (repository == null) {
+ if (repositories == null) {
repositories = new ArrayList<>();
}
repositories.add(repository);
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/pom.xml
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/pom.xml
index d508387a7358..82338ad07ae7 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/pom.xml
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/pom.xml
@@ -42,6 +42,18 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-yaml-dsl</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-core-engine</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-core-languages</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-stub</artifactId>
+ </dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/CamelYamlParser.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/CamelYamlParser.java
new file mode 100644
index 000000000000..be7b3a4f725b
--- /dev/null
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/CamelYamlParser.java
@@ -0,0 +1,86 @@
+/*
+ * 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.io.File;
+import java.nio.file.Files;
+import java.util.Collections;
+import java.util.List;
+
+import com.networknt.schema.ValidationMessage;
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.stub.StubComponent;
+import org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader;
+import org.apache.camel.dsl.yaml.validator.stub.StubBeanRepository;
+import org.apache.camel.dsl.yaml.validator.stub.StubDataFormat;
+import org.apache.camel.dsl.yaml.validator.stub.StubLanguage;
+import org.apache.camel.dsl.yaml.validator.stub.StubTransformer;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.ComponentResolver;
+import org.apache.camel.spi.DataFormatResolver;
+import org.apache.camel.spi.LanguageResolver;
+import org.apache.camel.spi.TransformerResolver;
+import org.apache.camel.support.DefaultRegistry;
+import org.apache.camel.support.ResourceHelper;
+
+/**
+ * Camel YAML parser that parses YAML DSL routes and also checks the routes
can be loaded by Camel. This parser does not
+ * start any routes, and will stub every component, dataformat, language which
would require to have all dependencies on
+ * classpath and 3rd party JARs may trigger some initialization that can
distort this parser.
+ *
+ * This is a faster and lighter parser than the {@link YamlValidator} which
uses a similar concept as in camel-jbang.
+ */
+public class CamelYamlParser {
+
+ public List<ValidationMessage> parse(File file) throws Exception {
+ CamelContext camelContext = null;
+ try {
+ DefaultRegistry registry = new DefaultRegistry();
+ registry.addBeanRepository(new StubBeanRepository());
+
+ camelContext = new DefaultCamelContext(registry);
+ camelContext.setAutoStartup(false);
+
camelContext.getCamelContextExtension().addContextPlugin(ComponentResolver.class,
+ (name, context) -> new StubComponent());
+
camelContext.getCamelContextExtension().addContextPlugin(DataFormatResolver.class,
+ (name, context) -> new StubDataFormat());
+
camelContext.getCamelContextExtension().addContextPlugin(LanguageResolver.class,
+ (name, context) -> new StubLanguage());
+
camelContext.getCamelContextExtension().addContextPlugin(TransformerResolver.class,
+ (name, context) -> new StubTransformer());
+ camelContext.start();
+
+ YamlRoutesBuilderLoader loader = new YamlRoutesBuilderLoader();
+ loader.setCamelContext(camelContext);
+ loader.start();
+
+ var rb =
loader.doLoadRouteBuilder(ResourceHelper.fromString(file.getName(),
Files.readString(file.toPath())));
+ camelContext.addRoutes(rb);
+ return Collections.emptyList();
+
+ } catch (Exception e) {
+ ValidationMessage vm = ValidationMessage.builder().type("parser")
+ .messageSupplier(() -> e.getClass().getName() + ": " +
e.getMessage()).build();
+ return List.of(vm);
+ } finally {
+ if (camelContext != null) {
+ camelContext.stop();
+ }
+ }
+ }
+
+}
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubBeanRepository.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubBeanRepository.java
new file mode 100644
index 000000000000..9557db7d05f3
--- /dev/null
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubBeanRepository.java
@@ -0,0 +1,83 @@
+/*
+ * 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.stub;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.camel.processor.DefaultClaimCheckRepository;
+import org.apache.camel.processor.aggregate.MemoryAggregationRepository;
+import org.apache.camel.spi.AggregationRepository;
+import org.apache.camel.spi.BeanRepository;
+import org.apache.camel.spi.ClaimCheckRepository;
+import org.apache.camel.spi.IdempotentRepository;
+import org.apache.camel.spi.StateRepository;
+import
org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository;
+import org.apache.camel.support.processor.state.MemoryStateRepository;
+
+public class StubBeanRepository implements BeanRepository {
+
+ @Override
+ public Object lookupByName(String name) {
+ return null;
+ }
+
+ @Override
+ public <T> T lookupByNameAndType(String name, Class<T> type) {
+ return stubType(type);
+ }
+
+ @Override
+ public <T> Map<String, T> findByTypeWithName(Class<T> type) {
+ T answer = stubType(type);
+ if (answer != null) {
+ // generate dummy name
+ String name = UUID.randomUUID().toString();
+ return Map.of(name, answer);
+ }
+ return Collections.EMPTY_MAP;
+ }
+
+ @Override
+ public <T> Set<T> findByType(Class<T> type) {
+ T answer = stubType(type);
+ if (answer != null) {
+ return Set.of(answer);
+ }
+ return Collections.EMPTY_SET;
+ }
+
+ private <T> T stubType(Class<T> type) {
+ // add repositories and other stuff we need to stub out, so they run
noop/in-memory only
+ // and do not start live connections to databases or other services
+ if (IdempotentRepository.class.isAssignableFrom(type)) {
+ return (T) new MemoryIdempotentRepository();
+ }
+ if (AggregationRepository.class.isAssignableFrom(type)) {
+ return (T) new MemoryAggregationRepository();
+ }
+ if (ClaimCheckRepository.class.isAssignableFrom(type)) {
+ return (T) new DefaultClaimCheckRepository();
+ }
+ if (StateRepository.class.isAssignableFrom(type)) {
+ return (T) new MemoryStateRepository();
+ }
+ return null;
+ }
+}
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubDataFormat.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubDataFormat.java
new file mode 100644
index 000000000000..b2669127c675
--- /dev/null
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubDataFormat.java
@@ -0,0 +1,48 @@
+/*
+ * 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.stub;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.spi.PropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerAware;
+import org.apache.camel.support.service.ServiceSupport;
+
+/**
+ * A data format that does nothing
+ */
+public class StubDataFormat extends ServiceSupport implements DataFormat,
PropertyConfigurerAware {
+
+ @Override
+ public void marshal(Exchange exchange, Object graph, OutputStream stream)
throws Exception {
+ // noop
+ }
+
+ @Override
+ public Object unmarshal(Exchange exchange, InputStream stream) throws
Exception {
+ return null;
+ }
+
+ @Override
+ public PropertyConfigurer getPropertyConfigurer(Object instance) {
+ // dummy configurer tha does nothing
+ return (camelContext, target, name, value, ignoreCase) -> true;
+ }
+}
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubLanguage.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubLanguage.java
new file mode 100644
index 000000000000..c7f36aaab382
--- /dev/null
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubLanguage.java
@@ -0,0 +1,40 @@
+/*
+ * 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.stub;
+
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.builder.ExpressionBuilder;
+import org.apache.camel.builder.PredicateBuilder;
+import org.apache.camel.spi.Language;
+import org.apache.camel.support.service.ServiceSupport;
+
+/**
+ * A language that does nothing
+ */
+public class StubLanguage extends ServiceSupport implements Language {
+
+ @Override
+ public Predicate createPredicate(String expression) {
+ return PredicateBuilder.constant(true);
+ }
+
+ @Override
+ public Expression createExpression(String expression) {
+ return ExpressionBuilder.constantExpression(expression);
+ }
+}
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubTransformer.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubTransformer.java
new file mode 100644
index 000000000000..c36447a1f5f2
--- /dev/null
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/stub/StubTransformer.java
@@ -0,0 +1,32 @@
+/*
+ * 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.stub;
+
+import org.apache.camel.Message;
+import org.apache.camel.spi.DataType;
+import org.apache.camel.spi.Transformer;
+
+/**
+ * A transformer that does nothing
+ */
+public class StubTransformer extends Transformer {
+
+ @Override
+ public void transform(Message message, DataType from, DataType to) throws
Exception {
+ // noop
+ }
+}
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/CamelYamlParserTest.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/CamelYamlParserTest.java
new file mode 100644
index 000000000000..21c58c494e7c
--- /dev/null
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/test/java/org/apache/camel/dsl/yaml/validator/CamelYamlParserTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.io.File;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+public class CamelYamlParserTest {
+
+ private static CamelYamlParser parser;
+
+ @BeforeAll
+ public static void setup() throws Exception {
+ parser = new CamelYamlParser();
+ }
+
+ @Test
+ public void testParseOk() throws Exception {
+ Assertions.assertTrue(parser.parse(new
File("src/test/resources/foo.yaml")).isEmpty());
+ }
+
+ @Test
+ public void testParseBad() throws Exception {
+ var report = parser.parse(new File("src/test/resources/bad.yaml"));
+ Assertions.assertFalse(report.isEmpty());
+ Assertions.assertEquals(1, report.size());
+ Assertions.assertTrue(report.get(0).getMessage().contains("Unknown
node id: setCheese"));
+ Assertions.assertTrue(report.get(0).getMessage().contains("-
setCheese:"));
+ }
+}