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

[Backport 8.12] Add test for big numbers in JSON parsing #734

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

package co.elastic.clients.json;

import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.json.jsonb.JsonbJsonpMapper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import jakarta.json.JsonArray;
import jakarta.json.JsonNumber;
import jakarta.json.bind.spi.JsonbProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import org.eclipse.parsson.JsonProviderImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -46,16 +49,18 @@ public class JsonpMapperTest extends Assertions {

@Test
public void testJsonb() {
JsonpMapper mapper = new JsonbJsonpMapper();
JsonpMapper mapper = new JsonbJsonpMapper(new JsonProviderImpl(), JsonbProvider.provider());
testSerialize(mapper, json);
testDeserialize(mapper, json);
testLargeNumber(mapper);
}

@Test
public void testJackson() {
JacksonJsonpMapper mapper = new JacksonJsonpMapper();
testSerialize(new JacksonJsonpMapper(), json);
testDeserialize(new JacksonJsonpMapper(), json);
testSerialize(mapper, json);
testDeserialize(mapper, json);
testLargeNumber(mapper);
}

@Test
Expand Down Expand Up @@ -149,6 +154,20 @@ private void testDeserialize(JsonpMapper mapper, String json) {
assertNull(child.getChildren());
}

private void testLargeNumber(JsonpMapper mapper) {
String json = "[1e999999]";
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
parser.next();

JsonArray array = parser.getValue().asJsonArray();

JsonNumber number = array.getJsonNumber(0);

assertTrue(Double.isInfinite(number.doubleValue()));

Assertions.assertThrows(Exception.class, number::bigIntegerValue);
}

public static class SomeClass {
private List<SomeClass> children;
private double doubleValue;
Expand Down
Loading