Skip to content

Fixed deserializing null String #411

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

Merged
merged 2 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
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 @@ -48,7 +48,7 @@ public ArangoDeserializerImpl(final VPack vpacker, final VPackParser vpackParser
public <T> T deserialize(final VPackSlice vpack, final Type type) throws ArangoDBException {
try {
final T doc;
if (type == String.class && !vpack.isString()) {
if (type == String.class && !vpack.isString() && !vpack.isNull()) {
doc = (T) vpackParser.toJson(vpack, true);
} else {
doc = vpacker.deserialize(vpack, type);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/arangodb/mapping/ArangoJack.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public VPackSlice serialize(final Object entity, final Options options) throws A
public <T> T deserialize(final VPackSlice vpack, final Type type) throws ArangoDBException {
try {
final T doc;
if (type == String.class && !vpack.isString()) {
if (type == String.class && !vpack.isString() && !vpack.isNull()) {
final JsonNode node = vpackMapper.readTree(
Arrays.copyOfRange(vpack.getBuffer(), vpack.getStart(), vpack.getStart() + vpack.getByteSize()));
doc = (T) jsonMapper.writeValueAsString(node);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/arangodb/ArangoDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ public void queryWithFailOnWarningTrue() {
public void queryWithFailOnWarningFalse() {
final ArangoCursor<String> cursor = db
.query("RETURN 1 / 0", null, new AqlQueryOptions().failOnWarning(false), String.class);
assertThat(cursor.next(), is("null"));
assertThat(cursor.next(), nullValue());
}

@Test
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/com/arangodb/serde/CustomSerdeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.arangodb.mapping.ArangoJack;
import com.arangodb.model.DocumentCreateOptions;
import com.arangodb.util.ArangoSerialization;
import com.arangodb.velocypack.VPackBuilder;
import com.arangodb.velocypack.VPackSlice;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
Expand All @@ -53,7 +54,7 @@
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;


/**
Expand Down Expand Up @@ -234,4 +235,10 @@ public void getDocument() {
assertThat(result.getAttribute("int"), is(BigInteger.valueOf(10)));
}

@Test
public void parseNullString() {
final String json = arangoDB.util(CUSTOM).deserialize(new VPackBuilder().add((String) null).slice(), String.class);
assertThat(json, nullValue());
}

}
13 changes: 9 additions & 4 deletions src/test/java/com/arangodb/util/ArangoSerializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@

import com.arangodb.ArangoDB;
import com.arangodb.entity.BaseDocument;
import com.arangodb.velocypack.Type;
import com.arangodb.velocypack.VPackBuilder;
import com.arangodb.velocypack.VPackSlice;
import com.arangodb.velocypack.ValueType;
import com.arangodb.velocypack.*;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -36,6 +33,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

/**
* @author Mark Vollmary
Expand Down Expand Up @@ -102,4 +100,11 @@ public void parseJsonIncludeNull() {
final String json = util.deserialize(util.serialize(entity, new ArangoSerializer.Options()), String.class);
assertThat(json, is("{\"value\":[\"test\",null]}"));
}

@Test
public void parseNullString() {
final String json = util.deserialize(new VPackBuilder().add((String) null).slice(), String.class);
assertThat(json, nullValue());
}

}