Skip to content

GsonMappingProvider does not return array of Integers #2

@gonciarz

Description

@gonciarz

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions