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

[DE-968] Support JSON-P types #593

Merged
merged 4 commits into from
Jan 9, 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
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<artifactId>jackson-annotations</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jakarta-jsonp</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.arangodb.arch.UsedInApi;
import com.arangodb.serde.ArangoSerde;
import com.arangodb.ContentType;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsonp.JSONPModule;
import jakarta.json.JsonException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -27,6 +31,7 @@
import static com.arangodb.internal.serde.SerdeUtils.extractBytes;

final class InternalSerdeImpl implements InternalSerde {
private static final Logger LOG = LoggerFactory.getLogger(InternalSerdeImpl.class);

static {
checkSupportedJacksonVersion();
Expand All @@ -50,6 +55,13 @@ final class InternalSerdeImpl implements InternalSerde {
new UserDataSerializer(this),
new UserDataDeserializer(this)
));

// JSON-P datatypes
try {
mapper.registerModule(new JSONPModule());
} catch (JsonException e) {
LOG.debug("Jakarta JSON-P provider not found, handling of JSON-P datatypes is disabled", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import jakarta.json.JsonValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -127,7 +128,8 @@ public static byte[] extractBytes(JsonParser parser) throws IOException {
}

public static boolean isManagedClass(Class<?> clazz) {
return JsonNode.class.isAssignableFrom(clazz) ||
return JsonNode.class.isAssignableFrom(clazz) || // jackson datatypes
JsonValue.class.isAssignableFrom(clazz) || // JSON-B datatypes
RawJson.class.equals(clazz) ||
RawBytes.class.equals(clazz) ||
BaseDocument.class.equals(clazz) ||
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.arangodb</groupId>
<artifactId>arangodb-java-driver</artifactId>
Expand Down Expand Up @@ -186,6 +191,7 @@
<scopes>compile</scopes>
<maxJdkVersion>1.8</maxJdkVersion>
<excludes>
<exclude>jakarta.json:jakarta.json-api</exclude>
<exclude>jakarta.json.bind:jakarta.json.bind-api</exclude>
</excludes>
</enforceBytecodeVersion>
Expand Down
6 changes: 6 additions & 0 deletions shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<!-- build arangodb-java-driver before so that we can use the javadoc artifact -->
<groupId>com.arangodb</groupId>
Expand Down Expand Up @@ -82,6 +87,7 @@
<artifactSet>
<excludes>
<exclude>org.slf4j:slf4j-api</exclude>
<exclude>jakarta.json:jakarta.json-api</exclude>
</excludes>
</artifactSet>
<relocations>
Expand Down
27 changes: 24 additions & 3 deletions test-functional/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<version>1.1.7</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>shaded</id>
Expand All @@ -34,11 +43,11 @@
<configuration>
<filesToExclude>
**/CustomSerdeTest.**,
**/SerdeTest.**,
**/SerializableTest.**,
**/CustomSerdeAsyncTest.**,
**/JacksonInterferenceTest.**,
**/JacksonRequestContextTest.**,
**/HttpProxyTest.**
**/HttpProxyTest.**,
**/RequestContextTest.**
</filesToExclude>
<replacements>
<replacement>
Expand All @@ -49,6 +58,18 @@
<token>com.fasterxml.jackson.databind.ObjectNode</token>
<value>com.arangodb.shaded.fasterxml.jackson.databind.ObjectNode</value>
</replacement>
<replacement>
<token>com.fasterxml.jackson.databind.node</token>
<value>com.arangodb.shaded.fasterxml.jackson.databind.node</value>
</replacement>
<replacement>
<token>com.fasterxml.jackson.databind.ObjectMapper</token>
<value>com.arangodb.shaded.fasterxml.jackson.databind.ObjectMapper</value>
</replacement>
<replacement>
<token>com.fasterxml.jackson.core.JsonProcessingException</token>
<value>com.arangodb.shaded.fasterxml.jackson.core.JsonProcessingException</value>
</replacement>
</replacements>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@


/**
* @author Michele Rastelli
* NB: excluded from shaded tests
*/
class HttpProxyTest extends BaseTest {

Expand Down
2 changes: 1 addition & 1 deletion test-functional/src/test/java/com/arangodb/BaseJunit5.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static ArangoDatabase initDB(String name) {
return database;
}

static ArangoDatabase initDB() {
protected static ArangoDatabase initDB() {
return initDB(TEST_DB);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Michele Rastelli
* NB: excluded from shaded tests
*/
class JacksonRequestContextTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Michele Rastelli
* NB: excluded from shaded tests
*/
class RequestContextTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@


/**
* @author Michele Rastelli
* NB: excluded from shaded tests
*/
class CustomSerdeAsyncTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@


/**
* @author Michele Rastelli
* NB: excluded from shaded tests
*/
class CustomSerdeTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import static org.assertj.core.api.Assertions.assertThat;

/**
* NB: excluded from shaded tests
*/
class JacksonInterferenceTest {

private final ObjectMapper mapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.arangodb.serde;

import com.arangodb.ArangoDatabase;
import com.arangodb.BaseJunit5;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

public class JsonBTypesTest extends BaseJunit5 {

@BeforeAll
static void init() {
BaseJunit5.initDB();
}

@ParameterizedTest
@MethodSource("dbs")
void jsonNode(ArangoDatabase db) {
JsonObject doc = Json.createObjectBuilder()
.add("foo", "bar")
.build();
JsonObject res = db.query("return @d", JsonObject.class, Collections.singletonMap("d", doc)).next();
assertThat(res.size()).isEqualTo(1);
assertThat(res.getString("foo")).isEqualTo("bar");
JsonValue value = db.query("return @d.foo", JsonValue.class, Collections.singletonMap("d", doc)).next();
assertThat(value)
.isInstanceOf(JsonString.class)
.extracting(v -> ((JsonString) v).getString())
.isEqualTo("bar");
}

}
1 change: 1 addition & 0 deletions test-functional/src/test/resources/simplelogger.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ org.slf4j.simpleLogger.showShortLogName=false
org.slf4j.simpleLogger.defaultLogLevel=info
#org.slf4j.simpleLogger.log.com.arangodb.internal.serde.JacksonUtils=debug
#org.slf4j.simpleLogger.log.com.arangodb.internal.net.Communication=debug
#org.slf4j.simpleLogger.log.com.arangodb.internal.serde.InternalSerdeImpl=debug