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

GsonMappingProvider does not return array of Integers #2

Open
gonciarz opened this issue Aug 11, 2016 · 1 comment
Open

GsonMappingProvider does not return array of Integers #2

gonciarz opened this issue Aug 11, 2016 · 1 comment

Comments

@gonciarz
Copy link

ClassPath:
com.revinate:assertj-json:1.0.1
com.jayway.jsonpath:json-path:2.0.0
com.google.code.gson:gson:2.7

Code to reproduce:

String json = "{"id":186,"areaIds":[39]}";

Configuration config = Configuration.builder()
.jsonProvider(new GsonJsonProvider())
.mappingProvider(new GsonMappingProvider())
.build();

DocumentContext response = JsonPath.using(config).parse(content);

JsonPathAssert.assertThat(response)
.jsonPathAsListOf("$.areaIds", Integer.class)
.containsExactly(39);

java.lang.AssertionError:
Expecting:
<[39.0]>
to contain exactly (and in same order):
<[39]>
but some elements were not found:
<[39]>
and others were not expected:
<[39.0]>

After a quick investigation:

com.jayway.jsonpath.spi.mapper.GsonMappingProvider

@OverRide
public T map(Object source, TypeRef targetType, Configuration configuration) { // source "[47]"
try {
return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source); // result "[47.0]"
} catch (Exception e){
throw new MappingException(e);
}
}

com.google.gson.internal.bind,ObjectTypeAdapter
JsonToken token is a NUMBER and ObjectTypeAdapter will always return Double.

case NUMBER: // * A JSON number represented in this API by a Java {@code double}, {@code long}, or {@code int}.
return in.nextDouble();

@jrglee
Copy link
Contributor

jrglee commented Aug 11, 2016

Hey @gonciarz

This is interesting, have you trying a newer version of json-path?

I looked for open issues in the jayway/JsonPath project and I did not find any one that matches this description. The number conversion seems to be done by this method https://github.com/jayway/JsonPath/blob/json-path-2.0.0/json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java#L74 but I am not familiar enough with GSON to know if it is doing something wrong.

This little utility was extracted from codebases that used Jackson and it has been working pretty well. If Jackson is not an option for you then I will investigate a little deeper and probably open an issue for the jayway/JsonPath project. PRs are welcomed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants