Browse Source

Fixed InvalidPathException bug.

pull/7/head
Kalle Stenflo 13 years ago
parent
commit
5b56bcf8dc
  1. 6
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  3. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/PathToken.java
  4. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  5. 5
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

6
json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java

@ -2,6 +2,7 @@ package com.jayway.jsonassert.impl;
import com.jayway.jsonassert.JsonAsserter; import com.jayway.jsonassert.JsonAsserter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
@ -53,10 +54,11 @@ public class JsonAsserterImpl implements JsonAsserter {
* {@inheritDoc} * {@inheritDoc}
*/ */
public JsonAsserter assertNotDefined(String path) { public JsonAsserter assertNotDefined(String path) {
Object o = JsonPath.read(jsonObject, path);
if (o != null) { try {
Object o = JsonPath.read(jsonObject, path);
throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path)); throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path));
} catch (InvalidPathException e) {
} }
return this; return this;
} }

2
json-path/src/main/java/com/jayway/jsonpath/JsonModel.java

@ -278,7 +278,7 @@ public class JsonModel {
PathToken currentToken; PathToken currentToken;
do { do {
currentToken = tokens.poll(); currentToken = tokens.poll();
modelRef = currentToken.get(modelRef, jsonProvider); modelRef = currentToken.apply(modelRef, jsonProvider);
} while (!tokens.isEmpty()); } while (!tokens.isEmpty());
if (modelRef.getClass().isAssignableFrom(clazz)) { if (modelRef.getClass().isAssignableFrom(clazz)) {

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

@ -37,7 +37,7 @@ public class PathToken {
return FilterFactory.createFilter(fragment).filter(model, jsonProvider); return FilterFactory.createFilter(fragment).filter(model, jsonProvider);
} }
public Object get(Object model, JsonProvider jsonProvider){ public Object apply(Object model, JsonProvider jsonProvider){
return FilterFactory.createFilter(fragment).getRef(model, jsonProvider); return FilterFactory.createFilter(fragment).getRef(model, jsonProvider);
} }

8
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java

@ -14,6 +14,7 @@
*/ */
package com.jayway.jsonpath.internal.filter; package com.jayway.jsonpath.internal.filter;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import java.util.List; import java.util.List;
@ -51,7 +52,12 @@ public class FieldFilter extends Filter {
return result; return result;
} }
} else { } else {
return jsonProvider.getMapValue(obj, condition); Map<String, Object> map = jsonProvider.toMap(obj);
if(!map.containsKey(condition)){
throw new InvalidPathException("invalid path");
} else {
return map.get(condition);
}
} }
} }

5
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -85,6 +85,11 @@ public class JsonModelTest {
} }
@Test(expected = InvalidPathException.class)
public void invalid_path_throws() throws Exception {
JsonModel.create(DOCUMENT).get("store.invalid");
}
} }

Loading…
Cancel
Save