Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-21306: camel-jsonpath - Null should be accepted return value #15812

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}