Skip to content

Commit

Permalink
Fix Issue #497 and add a testcase (#699)
Browse files Browse the repository at this point in the history
* The method getPropertyKeys() in class JsonOrgJsonProvider doesn't check empty jsonObject.

* Addition testcase

* simplify the implementation. And change method names of testcases to something more descriptive

* change names
  • Loading branch information
Pigdrum committed Jun 2, 2021
1 parent 727d9e0 commit 7384e96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class JsonOrgJsonProvider extends AbstractJsonProvider {

Expand Down Expand Up @@ -143,17 +144,14 @@ public boolean isMap(Object obj) {
return (obj instanceof JSONObject);
}

@SuppressWarnings("unchecked")
@Override
public Collection<String> getPropertyKeys(Object obj) {
JSONObject jsonObject = toJsonObject(obj);
List<String> keys = new ArrayList<String>();
try {
for (int i = 0; i < jsonObject.names().length(); i++) {
String key = (String) jsonObject.names().get(i);
keys.add(key);

}
return keys;
if(Objects.isNull(jsonObject.names()))
return new ArrayList<>();
return jsonObject.keySet();
} catch (JSONException e) {
throw new JsonPathException(e);
}
Expand Down
@@ -1,5 +1,7 @@
package com.jayway.jsonpath;

import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
import com.jayway.jsonpath.spi.mapper.JsonOrgMappingProvider;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
Expand Down Expand Up @@ -76,4 +78,23 @@ public void read_book_length() {
assertThat(result).isEqualTo(4);
}

@Test
public void test_getPropertyKeys_empty_object() {
String json = "{\"foo\": \"bar\", \"emptyObject\": {},\"emptyList\":[]}";
Configuration config = Configuration.defaultConfiguration()
.jsonProvider(new JsonOrgJsonProvider())
.mappingProvider(new JsonOrgMappingProvider());
Object result = JsonPath.using(config).parse(json).read("$..foo");
assertThat(result.toString()).isEqualTo("[\"bar\"]");
}

@Test
public void test_getPropertyKeys_empty_nest_object() {
String json = "{\"foo\": \"bar\", \"emptyObject\": {\"emptyList\":[]},\"emptyList\":[]}";
Configuration config = Configuration.defaultConfiguration()
.jsonProvider(new JsonOrgJsonProvider())
.mappingProvider(new JsonOrgMappingProvider());
Object result = JsonPath.using(config).parse(json).read("$..foo");
assertThat(result.toString()).isEqualTo("[\"bar\"]");
}
}

0 comments on commit 7384e96

Please sign in to comment.