Skip to content

Commit

Permalink
CAMEL-21306: camel-jsonpath - Null should be accepted return value (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Oct 3, 2024
1 parent 28d1f47 commit 8c34eb7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 8c34eb7

Please sign in to comment.