From 20ec2e9c64b417e2a4b98b7bda8ac909b02f6996 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Wed, 2 Oct 2024 19:48:37 +0200 Subject: [PATCH] CAMEL-21306: camel-jsonpath - Null should be accepted return value --- .../apache/camel/jsonpath/JsonPathEngine.java | 6 +- .../jsonpath/JsonPathNullHeaderTest.java | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathNullHeaderTest.java diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java index afbdc83b699fa..ebe49dd9eab0e 100644 --- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java +++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java @@ -194,15 +194,15 @@ private Object doRead(String path, Exchange exchange) throws IOException, CamelE if (json instanceof String) { LOG.trace("JSonPath: {} is read as String: {}", path, json); String str = (String) json; - answer = JsonPath.using(configuration).parse(str).read(path); + return JsonPath.using(configuration).parse(str).read(path); } else if (json instanceof Map) { LOG.trace("JSonPath: {} is read as Map: {}", path, json); Map map = (Map) json; - answer = JsonPath.using(configuration).parse(map).read(path); + return JsonPath.using(configuration).parse(map).read(path); } else if (json instanceof List) { LOG.trace("JSonPath: {} is read as List: {}", path, json); List list = (List) json; - answer = JsonPath.using(configuration).parse(list).read(path); + return JsonPath.using(configuration).parse(list).read(path); } else { //try to auto convert into inputStream answer = readWithInputStream(path, exchange); diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathNullHeaderTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathNullHeaderTest.java new file mode 100644 index 0000000000000..9362d89735f95 --- /dev/null +++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathNullHeaderTest.java @@ -0,0 +1,64 @@ +/* + * 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.jsonpath; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +public class JsonPathNullHeaderTest extends CamelTestSupport { + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start") + .setHeader("foo").jsonpath("$.foo") + .to("mock:foo"); + } + }; + } + + @Test + public void testHeaderValue() throws Exception { + getMockEndpoint("mock:foo").expectedHeaderReceived("foo", "cheese"); + + template.sendBody("direct:start", """ + { + "foo": "cheese" + } + """); + + MockEndpoint.assertIsSatisfied(context); + } + + @Test + public void testHeaderNull() throws Exception { + getMockEndpoint("mock:foo").expectedHeaderReceived("foo", null); + + template.sendBody("direct:start", """ + { + "foo": null + } + """); + + MockEndpoint.assertIsSatisfied(context); + } + +}