Skip to content

Commit 979b800

Browse files
committed
feat: don't use jackson-databind-nullable to fix serialization problems with Jackson default configuration
1 parent 8ffbebd commit 979b800

File tree

6 files changed

+68
-45
lines changed

6 files changed

+68
-45
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[versions]
22
jackson = "2.17.2"
3-
jackson-databind-nullable = "0.2.6"
43
jakarta-annotation-api = "2.0.0"
54
jersey = "3.1.7"
65
junit = "5.10.2"
@@ -12,7 +11,6 @@ swagger-annotations = "2.2.22"
1211
jackson-annotations = { module = "com.fasterxml.jackson.core:jackson-annotations", version.ref = "jackson" }
1312
jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref = "jackson" }
1413
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
15-
jackson-databind-nullable = { module = "org.openapitools:jackson-databind-nullable", version.ref = "jackson-databind-nullable" }
1614
jackson-jsr310 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310", version.ref = "jackson" }
1715
jakarta-annotation-api = { module = "jakarta.annotation:jakarta.annotation-api", version.ref = "jakarta-annotation-api" }
1816
jersey-apache-connector = { module = "org.glassfish.jersey.connectors:jersey-apache-connector", version.ref = "jersey" }
@@ -26,4 +24,4 @@ mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }
2624
swagger-annotations = { module = "io.swagger.core.v3:swagger-annotations", version.ref = "swagger-annotations" }
2725

2826
[plugins]
29-
openapi-generator = { id = "org.openapi.generator", version.ref = "openapi" }
27+
openapi-generator = { id = "org.openapi.generator", version.ref = "openapi" }

sdk/sdk.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ dependencies {
3535
api(libs.jackson.core)
3636
api(libs.jackson.annotations)
3737
api(libs.jackson.databind)
38-
api(libs.jackson.databind.nullable)
3938
api(libs.jackson.jsr310)
4039
api(libs.jakarta.annotation.api)
4140
testImplementation(libs.junit.jupiter.api)
@@ -68,6 +67,7 @@ openApiGenerate {
6867
gitRepoId.set("fingerprint-pro-server-api-java-sdk")
6968
gitUserId.set("fingerprintjs")
7069
configOptions.put("hideGenerationTimestamp", "true")
70+
configOptions.put("openApiNullable", "false")
7171
}
7272

7373
tasks.register("removeDocs") {
@@ -143,4 +143,4 @@ tasks.test {
143143

144144
tasks.jar {
145145
archiveBaseName = "fingerprint-pro-server-api-sdk"
146-
}
146+
}

sdk/src/main/java/com/fingerprint/model/RawDeviceAttributesResultValue.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
import com.fasterxml.jackson.annotation.JsonValue;
99
import com.fingerprint.model.Error;
1010
import java.util.Arrays;
11-
import org.openapitools.jackson.nullable.JsonNullable;
12-
import com.fasterxml.jackson.annotation.JsonIgnore;
13-
import org.openapitools.jackson.nullable.JsonNullable;
14-
import java.util.NoSuchElementException;
1511
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1612
import com.fingerprint.sdk.JSON;
1713
import io.swagger.v3.oas.annotations.media.Schema;
@@ -32,7 +28,7 @@ public class RawDeviceAttributesResultValue {
3228
private Error error;
3329

3430
public static final String JSON_PROPERTY_VALUE = "value";
35-
private JsonNullable<Object> value = JsonNullable.<Object>of(null);
31+
private Object value = null;
3632

3733
public RawDeviceAttributesResultValue() {
3834
}
@@ -64,7 +60,7 @@ public void setError(Error error) {
6460

6561

6662
public RawDeviceAttributesResultValue value(Object value) {
67-
this.value = JsonNullable.<Object>of(value);
63+
this.value = value;
6864
return this;
6965
}
7066

@@ -74,26 +70,18 @@ public RawDeviceAttributesResultValue value(Object value) {
7470
**/
7571
@jakarta.annotation.Nullable
7672
@Schema(description = "")
77-
@JsonIgnore
78-
79-
public Object getValue() {
80-
return value.orElse(null);
81-
}
82-
8373
@JsonProperty(JSON_PROPERTY_VALUE)
8474
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
8575

86-
public JsonNullable<Object> getValue_JsonNullable() {
76+
public Object getValue() {
8777
return value;
8878
}
89-
90-
@JsonProperty(JSON_PROPERTY_VALUE)
91-
public void setValue_JsonNullable(JsonNullable<Object> value) {
92-
this.value = value;
93-
}
9479

80+
81+
@JsonProperty(JSON_PROPERTY_VALUE)
82+
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
9583
public void setValue(Object value) {
96-
this.value = JsonNullable.<Object>of(value);
84+
this.value = value;
9785
}
9886

9987

@@ -110,23 +98,12 @@ public boolean equals(Object o) {
11098
}
11199
RawDeviceAttributesResultValue rawDeviceAttributesResultValue = (RawDeviceAttributesResultValue) o;
112100
return Objects.equals(this.error, rawDeviceAttributesResultValue.error) &&
113-
equalsNullable(this.value, rawDeviceAttributesResultValue.value);
114-
}
115-
116-
private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) {
117-
return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get()));
101+
Objects.equals(this.value, rawDeviceAttributesResultValue.value);
118102
}
119103

120104
@Override
121105
public int hashCode() {
122-
return Objects.hash(error, hashCodeNullable(value));
123-
}
124-
125-
private static <T> int hashCodeNullable(JsonNullable<T> a) {
126-
if (a == null) {
127-
return 1;
128-
}
129-
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
106+
return Objects.hash(error, value);
130107
}
131108

132109
@Override

sdk/src/main/java/com/fingerprint/sdk/JSON.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.annotation.*;
44
import com.fasterxml.jackson.databind.*;
55
import com.fasterxml.jackson.databind.json.JsonMapper;
6-
import org.openapitools.jackson.nullable.JsonNullableModule;
76
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
87
import com.fingerprint.model.*;
98

@@ -30,8 +29,6 @@ public JSON() {
3029
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
3130
mapper.setDateFormat(new RFC3339DateFormat());
3231
mapper.registerModule(new JavaTimeModule());
33-
JsonNullableModule jnm = new JsonNullableModule();
34-
mapper.registerModule(jnm);
3532
}
3633

3734
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.fingerprint;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.SerializationFeature;
5+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
6+
import com.fingerprint.model.EventResponse;
7+
import com.fingerprint.model.RawDeviceAttributesResultValue;
8+
import com.fingerprint.model.SignalResponseRawDeviceAttributes;
9+
import com.fingerprint.sdk.ApiException;
10+
import com.fingerprint.sdk.JSON;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.TestInstance;
13+
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
21+
public class SerializationTest {
22+
23+
private InputStream getFileAsIOStream(final String fileName) {
24+
InputStream ioStream = this.getClass()
25+
.getClassLoader()
26+
.getResourceAsStream(fileName);
27+
28+
if (ioStream == null) {
29+
throw new IllegalArgumentException(fileName + " is not found");
30+
}
31+
return ioStream;
32+
}
33+
34+
private static ObjectMapper getMapper() {
35+
ObjectMapper mapper = new ObjectMapper();
36+
mapper.registerModule(new JavaTimeModule());
37+
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
38+
// mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
39+
return mapper;
40+
}
41+
42+
@Test
43+
public void serializeRawDeviceAttributesTest() throws IOException {
44+
ObjectMapper sdkObjectMapper = JSON.getDefault().getMapper();
45+
EventResponse eventResponse = sdkObjectMapper.readValue(getFileAsIOStream("mocks/get_event_200.json"), EventResponse.class);
46+
47+
SignalResponseRawDeviceAttributes signalResponseRawDeviceAttributes = eventResponse.getProducts().getRawDeviceAttributes();
48+
String sdkResult = sdkObjectMapper.writeValueAsString(signalResponseRawDeviceAttributes);
49+
50+
ObjectMapper springLikeObjectMapper = getMapper();
51+
String springResult = springLikeObjectMapper.writeValueAsString(signalResponseRawDeviceAttributes);
52+
53+
assertTrue(sdkResult.contains("\"architecture\":{\"value\":127}"));
54+
assertTrue(springResult.contains("\"architecture\":{\"error\":null,\"value\":127}"));
55+
}
56+
}

sdk/src/test/java/com/fingerprint/api/FingerprintApiTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import org.junit.jupiter.api.TestInstance;
1717
import org.mockito.Mockito;
18-
import org.openapitools.jackson.nullable.JsonNullableModule;
1918

2019
import static org.junit.jupiter.api.Assertions.*;
2120
import static org.mockito.Mockito.when;
@@ -69,8 +68,6 @@ private static ObjectMapper getMapper() {
6968
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
7069
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
7170
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
72-
JsonNullableModule jnm = new JsonNullableModule();
73-
mapper.registerModule(jnm);
7471
return mapper;
7572
}
7673

@@ -263,8 +260,6 @@ public void getVisitsTest() throws ApiException {
263260
public void webhookTest() throws Exception {
264261
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
265262
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
266-
JsonNullableModule jnm = new JsonNullableModule();
267-
mapper.registerModule(jnm);
268263

269264
WebhookVisit visit = mapper.readValue(getFileAsIOStream("mocks/webhook.json"), WebhookVisit.class);
270265

0 commit comments

Comments
 (0)