You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/08/24 07:19:11 UTC
(camel) branch main updated: CAMEL-22368: camel-catalog - Add support for validating JQ language
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 b99464d79ef CAMEL-22368: camel-catalog - Add support for validating JQ language
b99464d79ef is described below
commit b99464d79eff6c6e2cdfe840a7d292f6b61eb3b2
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Aug 24 09:09:07 2025 +0200
CAMEL-22368: camel-catalog - Add support for validating JQ language
---
catalog/camel-catalog/pom.xml | 6 ++++
.../org/apache/camel/catalog/CamelCatalogTest.java | 12 +++++++
.../org/apache/camel/language/jq/JqExpression.java | 2 +-
.../org/apache/camel/language/jq/JqFunctions.java | 4 +--
.../org/apache/camel/language/jq/JqLanguage.java | 31 +++++++++++++----
.../camel/language/jq/JqLanguageValidateTest.java | 40 ++++++++++++++++++++++
6 files changed, 86 insertions(+), 9 deletions(-)
diff --git a/catalog/camel-catalog/pom.xml b/catalog/camel-catalog/pom.xml
index 574407f4f29..af7bcf32092 100644
--- a/catalog/camel-catalog/pom.xml
+++ b/catalog/camel-catalog/pom.xml
@@ -104,6 +104,12 @@
<artifactId>camel-jsonpath</artifactId>
<scope>test</scope>
</dependency>
+ <!-- for testing jq language -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-jq</artifactId>
+ <scope>test</scope>
+ </dependency>
<!-- for testing groovy language -->
<dependency>
<groupId>org.apache.camel</groupId>
diff --git a/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java b/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
index e2636a2c1a3..4b777b25f80 100644
--- a/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
+++ b/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -1152,6 +1152,18 @@ public class CamelCatalogTest {
assertEquals("$.store.book[?(@.price < 10)]", result.getText());
}
+ @Test
+ public void testValidateJQLanguage() {
+ LanguageValidationResult result = catalog.validateLanguagePredicate(null, "jq", ".foo == \"bar\"");
+ assertTrue(result.isSuccess());
+ assertEquals(".foo == \"bar\"", result.getText());
+
+ result = catalog.validateLanguageExpression(null, "jq", ". ^^+ [{\"array\": body()}]");
+ assertFalse(result.isSuccess());
+ assertEquals(". ^^+ [{\"array\": body()}]", result.getText());
+ assertEquals("Cannot compile query: . ^^+ [{\"array\": body()}]", result.getError());
+ }
+
@Test
public void testValidateGroovyLanguage() {
LanguageValidationResult result = catalog.validateLanguageExpression(null, "groovy", "4 * 3");
diff --git a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java
index 6f0029811d2..9501c72fa63 100644
--- a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java
+++ b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java
@@ -72,7 +72,7 @@ public class JqExpression extends ExpressionAdapter implements ExpressionResultT
this.typeConverter = camelContext.getTypeConverter();
try {
- this.query = JsonQuery.compile(this.expression, Versions.JQ_1_6);
+ this.query = JsonQuery.compile(this.expression, Versions.JQ_1_7);
} catch (JsonQueryException e) {
throw new ExpressionIllegalSyntaxException(this.expression, e);
}
diff --git a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqFunctions.java b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqFunctions.java
index e1b3ec86c19..1022447b136 100644
--- a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqFunctions.java
+++ b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqFunctions.java
@@ -53,14 +53,14 @@ public final class JqFunctions {
camelContext.getApplicationContextClassLoader() != null
? camelContext.getApplicationContextClassLoader()
: BuiltinFunctionLoader.class.getClassLoader(),
- Versions.JQ_1_6);
+ Versions.JQ_1_7);
Map<String, Function> fromJq = BuiltinFunctionLoader.getInstance()
.loadFunctionsFromJsonJq(
camelContext.getApplicationContextClassLoader() != null
? camelContext.getApplicationContextClassLoader()
: BuiltinFunctionLoader.class.getClassLoader(),
- Versions.JQ_1_6,
+ Versions.JQ_1_7,
scope);
if (fromServiceLoader != null) {
diff --git a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java
index 4363ae94070..e21bfd984ac 100644
--- a/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java
+++ b/components/camel-jq/src/main/java/org/apache/camel/language/jq/JqLanguage.java
@@ -16,14 +16,15 @@
*/
package org.apache.camel.language.jq;
+import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.jackson.jq.Scope;
+import net.thisptr.jackson.jq.Versions;
import net.thisptr.jackson.jq.module.loaders.BuiltinModuleLoader;
import org.apache.camel.Expression;
import org.apache.camel.StaticService;
import org.apache.camel.spi.annotations.Language;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.support.SingleInputTypedLanguageSupport;
-import org.apache.camel.util.ObjectHelper;
@Language("jq")
public class JqLanguage extends SingleInputTypedLanguageSupport implements StaticService {
@@ -32,16 +33,20 @@ public class JqLanguage extends SingleInputTypedLanguageSupport implements Stati
@Override
public void init() {
- ObjectHelper.notNull(getCamelContext(), "CamelContext", this);
-
- this.rootScope = CamelContextHelper.findSingleByType(getCamelContext(), Scope.class);
+ if (getCamelContext() != null) {
+ this.rootScope = CamelContextHelper.findSingleByType(getCamelContext(), Scope.class);
+ }
if (this.rootScope == null) {
this.rootScope = Scope.newEmptyScope();
this.rootScope.setModuleLoader(BuiltinModuleLoader.getInstance());
- JqFunctions.load(getCamelContext(), rootScope);
+ if (getCamelContext() != null) {
+ JqFunctions.load(getCamelContext(), rootScope);
+ }
}
- JqFunctions.loadFromRegistry(getCamelContext(), rootScope);
+ if (getCamelContext() != null) {
+ JqFunctions.loadFromRegistry(getCamelContext(), rootScope);
+ }
JqFunctions.loadLocal(rootScope);
}
@@ -70,4 +75,18 @@ public class JqLanguage extends SingleInputTypedLanguageSupport implements Stati
return answer;
}
+ // use by tooling
+ public boolean validateExpression(String expression) throws Exception {
+ init();
+ JsonQuery jq = JsonQuery.compile(expression, Versions.JQ_1_7);
+ return true;
+ }
+
+ // use by tooling
+ public boolean validatePredicate(String expression) throws Exception {
+ init();
+ JsonQuery.compile(expression, Versions.JQ_1_7);
+ return true;
+ }
+
}
diff --git a/components/camel-jq/src/test/java/org/apache/camel/language/jq/JqLanguageValidateTest.java b/components/camel-jq/src/test/java/org/apache/camel/language/jq/JqLanguageValidateTest.java
new file mode 100644
index 00000000000..84e5ca2e487
--- /dev/null
+++ b/components/camel-jq/src/test/java/org/apache/camel/language/jq/JqLanguageValidateTest.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.language.jq;
+
+import net.thisptr.jackson.jq.exception.JsonQueryException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.fail;
+
+public class JqLanguageValidateTest {
+
+ @Test
+ public void testValidateLanguage() throws Exception {
+ JqLanguage lan = new JqLanguage();
+ lan.init();
+ Assertions.assertTrue(lan.validateExpression(". + [{\"array\": body()}]"));
+ try {
+ Assertions.assertFalse(lan.validateExpression(". ^^+ [{\"array\": body()}]"));
+ fail();
+ } catch (JsonQueryException e) {
+ Assertions.assertTrue(e.getMessage().startsWith("Cannot compile query"));
+ }
+ }
+
+}