Browse Source

Merge pull request #118 from jochenberger/fix-require-properties-handling

fix handling of REQUIRE_PROPERTIES with nested objects
pull/133/head
kallestenflo 9 years ago
parent
commit
35b45f5e0b
  1. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java
  2. 19
      json-path/src/test/java/com/jayway/jsonpath/OptionsTest.java

3
json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java

@ -47,7 +47,8 @@ public abstract class PathToken {
if(ctx.options().contains(Option.DEFAULT_PATH_LEAF_TO_NULL)){
propertyVal = null;
} else {
if(ctx.options().contains(Option.SUPPRESS_EXCEPTIONS) && !ctx.options().contains(Option.REQUIRE_PROPERTIES)){
if(ctx.options().contains(Option.SUPPRESS_EXCEPTIONS) ||
!ctx.options().contains(Option.REQUIRE_PROPERTIES)){
return;
} else {
throw new PathNotFoundException("No results for path: " + evalPath);

19
json-path/src/test/java/com/jayway/jsonpath/OptionsTest.java

@ -100,4 +100,23 @@ public class OptionsTest extends BaseTest {
} catch (PathNotFoundException pnf){}
}
@Test
public void when_property_is_required_exception_is_thrown_2() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("a", singletonMap("a-key", "a-val"));
model.put("b", singletonMap("b-key", "b-val"));
Configuration conf = Configuration.defaultConfiguration();
assertThat(using(conf).parse(model).read("$.*.a-key", List.class)).containsExactly("a-val");
conf = conf.addOptions(Option.REQUIRE_PROPERTIES);
try{
using(conf).parse(model).read("$.*.a-key", List.class);
fail("Should throw PathNotFoundException");
} catch (PathNotFoundException pnf){}
}
}

Loading…
Cancel
Save