Skip to content

[DE-969] deserialization passthrough for RawBytes and RawJson #592

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
Jan 8, 2025
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 @@ -130,15 +130,6 @@ default <T> T deserialize(byte[] content, String jsonPointer, Type type) {
*/
<T> T deserializeUserData(byte[] content, JavaType clazz);

/**
* Deserializes the parsed json node and binds it to the target data type.
* The parser is not closed.
*
* @param parser json parser
* @param clazz class of target data type
* @return deserialized object
*/
<T> T deserializeUserData(JsonParser parser, JavaType clazz);

/**
* @param content byte array to deserialize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,6 @@ public <T> T deserializeUserData(byte[] content, JavaType clazz) {
}
}

@Override
public <T> T deserializeUserData(JsonParser parser, JavaType clazz) {
try {
if (SerdeUtils.isManagedClass(clazz.getRawClass())) {
return mapper.readerFor(clazz).readValue(parser);
} else {
return deserializeUserData(extractBytes(parser), clazz);
}
} catch (IOException e) {
throw ArangoDBException.of(e);
}
}

@Override
public boolean isDocument(byte[] content) {
try (JsonParser p = mapper.getFactory().createParser(content)) {
Expand Down Expand Up @@ -240,14 +227,21 @@ public <T> T deserialize(final JsonNode node, final Type type) {
}

@Override
@SuppressWarnings("unchecked")
public <T> T deserialize(final byte[] content, final Type type) {
if (content == null || content.length == 0) {
return null;
}
try {
return mapper.readerFor(mapper.constructType(type)).readValue(content);
} catch (IOException e) {
throw ArangoDBException.of(e);
if (RawBytes.class.equals(type)) {
return (T) RawBytes.of(content);
} else if (RawJson.class.equals(type) && JsonFactory.FORMAT_NAME_JSON.equals(mapper.getFactory().getFormatName())) {
return (T) RawJson.of(new String(content, StandardCharsets.UTF_8));
} else {
try {
return mapper.readerFor(mapper.constructType(type)).readValue(content);
} catch (IOException e) {
throw ArangoDBException.of(e);
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions test-resilience/src/test/java/resilience/mock/SerdeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import ch.qos.logback.classic.Level;
import com.arangodb.ArangoDBException;
import com.arangodb.Request;
import com.arangodb.Response;
import com.arangodb.entity.MultiDocumentEntity;
import com.arangodb.util.RawJson;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -133,4 +136,31 @@ void getDocumentsWithErrorField() {
.anySatisfy(d -> assertThat(d.get("_key").textValue()).isEqualTo("2"))
.anySatisfy(d -> assertThat(d.get("_key").textValue()).isEqualTo("3"));
}

@Test
void getXArangoDumpJsonLines() {
String resp = "{\"a\":1}\n" +
"{\"b\":2}\n" +
"{\"c\":3}";

mockServer
.when(
request()
.withMethod("GET")
.withPath("/_db/foo/_api/foo")
)
.respond(
response()
.withStatusCode(200)
.withHeader("Content-Type", "application/x-arango-dump; charset=utf-8")
.withBody(resp.getBytes(StandardCharsets.UTF_8))
);

Response<RawJson> res = arangoDB.execute(Request.builder()
.method(Request.Method.GET)
.db("foo")
.path("/_api/foo")
.build(), RawJson.class);
assertThat(res.getBody().get()).endsWith("{\"c\":3}");
}
}