Skip to content

Commit 738b9f5

Browse files
authored
[DE-969] deserialization passthrough for RawBytes and RawJson (#592)
* Serde: deserialization pass-through for RawJson and RawBytes * test x-arango-dump content-type
1 parent 71310c8 commit 738b9f5

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

Diff for: core/src/main/java/com/arangodb/internal/serde/InternalSerde.java

-9
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,6 @@ default <T> T deserialize(byte[] content, String jsonPointer, Type type) {
130130
*/
131131
<T> T deserializeUserData(byte[] content, JavaType clazz);
132132

133-
/**
134-
* Deserializes the parsed json node and binds it to the target data type.
135-
* The parser is not closed.
136-
*
137-
* @param parser json parser
138-
* @param clazz class of target data type
139-
* @return deserialized object
140-
*/
141-
<T> T deserializeUserData(JsonParser parser, JavaType clazz);
142133

143134
/**
144135
* @param content byte array to deserialize

Diff for: core/src/main/java/com/arangodb/internal/serde/InternalSerdeImpl.java

+11-17
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,6 @@ public <T> T deserializeUserData(byte[] content, JavaType clazz) {
180180
}
181181
}
182182

183-
@Override
184-
public <T> T deserializeUserData(JsonParser parser, JavaType clazz) {
185-
try {
186-
if (SerdeUtils.isManagedClass(clazz.getRawClass())) {
187-
return mapper.readerFor(clazz).readValue(parser);
188-
} else {
189-
return deserializeUserData(extractBytes(parser), clazz);
190-
}
191-
} catch (IOException e) {
192-
throw ArangoDBException.of(e);
193-
}
194-
}
195-
196183
@Override
197184
public boolean isDocument(byte[] content) {
198185
try (JsonParser p = mapper.getFactory().createParser(content)) {
@@ -240,14 +227,21 @@ public <T> T deserialize(final JsonNode node, final Type type) {
240227
}
241228

242229
@Override
230+
@SuppressWarnings("unchecked")
243231
public <T> T deserialize(final byte[] content, final Type type) {
244232
if (content == null || content.length == 0) {
245233
return null;
246234
}
247-
try {
248-
return mapper.readerFor(mapper.constructType(type)).readValue(content);
249-
} catch (IOException e) {
250-
throw ArangoDBException.of(e);
235+
if (RawBytes.class.equals(type)) {
236+
return (T) RawBytes.of(content);
237+
} else if (RawJson.class.equals(type) && JsonFactory.FORMAT_NAME_JSON.equals(mapper.getFactory().getFormatName())) {
238+
return (T) RawJson.of(new String(content, StandardCharsets.UTF_8));
239+
} else {
240+
try {
241+
return mapper.readerFor(mapper.constructType(type)).readValue(content);
242+
} catch (IOException e) {
243+
throw ArangoDBException.of(e);
244+
}
251245
}
252246
}
253247

Diff for: test-resilience/src/test/java/resilience/mock/SerdeTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import ch.qos.logback.classic.Level;
44
import com.arangodb.ArangoDBException;
5+
import com.arangodb.Request;
6+
import com.arangodb.Response;
57
import com.arangodb.entity.MultiDocumentEntity;
8+
import com.arangodb.util.RawJson;
69
import com.fasterxml.jackson.core.JsonParseException;
710
import com.fasterxml.jackson.databind.JsonNode;
811
import org.junit.jupiter.api.Test;
@@ -133,4 +136,31 @@ void getDocumentsWithErrorField() {
133136
.anySatisfy(d -> assertThat(d.get("_key").textValue()).isEqualTo("2"))
134137
.anySatisfy(d -> assertThat(d.get("_key").textValue()).isEqualTo("3"));
135138
}
139+
140+
@Test
141+
void getXArangoDumpJsonLines() {
142+
String resp = "{\"a\":1}\n" +
143+
"{\"b\":2}\n" +
144+
"{\"c\":3}";
145+
146+
mockServer
147+
.when(
148+
request()
149+
.withMethod("GET")
150+
.withPath("/_db/foo/_api/foo")
151+
)
152+
.respond(
153+
response()
154+
.withStatusCode(200)
155+
.withHeader("Content-Type", "application/x-arango-dump; charset=utf-8")
156+
.withBody(resp.getBytes(StandardCharsets.UTF_8))
157+
);
158+
159+
Response<RawJson> res = arangoDB.execute(Request.builder()
160+
.method(Request.Method.GET)
161+
.db("foo")
162+
.path("/_api/foo")
163+
.build(), RawJson.class);
164+
assertThat(res.getBody().get()).endsWith("{\"c\":3}");
165+
}
136166
}

0 commit comments

Comments
 (0)