Skip to content

Commit d897442

Browse files
committed
Implement Jackson3 support and maintain Jackson2 support
1 parent d0afaa6 commit d897442

36 files changed

+1388
-385
lines changed

pom.xml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
<!-- region Dependency Versions -->
3737
<!-- Validator 8+ requires Java 11, Validator 9+ requires Java 17. -->
3838
<hibernate-validator.version>7.0.5.Final</hibernate-validator.version>
39-
<jackson-bom.version>2.20.1</jackson-bom.version>
39+
<jackson2-bom.version>2.20.1</jackson2-bom.version>
40+
<jackson3-bom.version>3.0.4</jackson3-bom.version>
4041
<javax-validation-api.version>2.0.1.Final</javax-validation-api.version>
4142
<jakarta-validation-api.version>3.1.1</jakarta-validation-api.version>
4243
<junit.version>5.14.2</junit.version>
@@ -60,10 +61,17 @@
6061
<dependency>
6162
<groupId>com.fasterxml.jackson</groupId>
6263
<artifactId>jackson-bom</artifactId>
63-
<version>${jackson-bom.version}</version>
64+
<version>${jackson2-bom.version}</version>
6465
<scope>import</scope>
6566
<type>pom</type>
6667
</dependency>
68+
<dependency>
69+
<groupId>tools.jackson</groupId>
70+
<artifactId>jackson-bom</artifactId>
71+
<version>${jackson3-bom.version}</version>
72+
<type>pom</type>
73+
<scope>import</scope>
74+
</dependency>
6775
<dependency>
6876
<groupId>org.junit</groupId>
6977
<artifactId>junit-bom</artifactId>
@@ -78,6 +86,12 @@
7886
<dependency>
7987
<groupId>com.fasterxml.jackson.core</groupId>
8088
<artifactId>jackson-databind</artifactId>
89+
<scope>provided</scope>
90+
</dependency>
91+
<dependency>
92+
<groupId>tools.jackson.core</groupId>
93+
<artifactId>jackson-databind</artifactId>
94+
<scope>provided</scope>
8195
</dependency>
8296
<dependency>
8397
<groupId>javax.validation</groupId>

src/main/java/org/openapitools/jackson/nullable/JsonNullableBeanPropertyWriter.java renamed to src/main/java/org/openapitools/jackson/nullable/JsonNullableJackson2BeanPropertyWriter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
77
import com.fasterxml.jackson.databind.util.NameTransformer;
88

9-
public class JsonNullableBeanPropertyWriter extends BeanPropertyWriter
9+
public class JsonNullableJackson2BeanPropertyWriter extends BeanPropertyWriter
1010
{
1111
private static final long serialVersionUID = 1L;
1212

13-
protected JsonNullableBeanPropertyWriter(BeanPropertyWriter base) {
13+
protected JsonNullableJackson2BeanPropertyWriter(BeanPropertyWriter base) {
1414
super(base);
1515
}
1616

17-
protected JsonNullableBeanPropertyWriter(BeanPropertyWriter base, PropertyName newName) {
17+
protected JsonNullableJackson2BeanPropertyWriter(BeanPropertyWriter base, PropertyName newName) {
1818
super(base, newName);
1919
}
2020

2121
@Override
2222
protected BeanPropertyWriter _new(PropertyName newName) {
23-
return new JsonNullableBeanPropertyWriter(this, newName);
23+
return new JsonNullableJackson2BeanPropertyWriter(this, newName);
2424
}
2525

2626
@Override
2727
public BeanPropertyWriter unwrappingWriter(NameTransformer unwrapper) {
28-
return new UnwrappingJsonNullableBeanPropertyWriter(this, unwrapper);
28+
return new UnwrappingJsonNullableJackson2BeanPropertyWriter(this, unwrapper);
2929
}
3030

3131
@Override

src/main/java/org/openapitools/jackson/nullable/JsonNullableBeanSerializerModifier.java renamed to src/main/java/org/openapitools/jackson/nullable/JsonNullableJackson2BeanSerializerModifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.util.List;
1010

11-
public class JsonNullableBeanSerializerModifier extends BeanSerializerModifier
11+
public class JsonNullableJackson2BeanSerializerModifier extends BeanSerializerModifier
1212
{
1313
@Override
1414
public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
@@ -19,7 +19,7 @@ public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
1919
final BeanPropertyWriter writer = beanProperties.get(i);
2020
JavaType type = writer.getType();
2121
if (type.isTypeOrSubTypeOf(JsonNullable.class)) {
22-
beanProperties.set(i, new JsonNullableBeanPropertyWriter(writer));
22+
beanProperties.set(i, new JsonNullableJackson2BeanPropertyWriter(writer));
2323
}
2424
}
2525
return beanProperties;

src/main/java/org/openapitools/jackson/nullable/JsonNullableDeserializer.java renamed to src/main/java/org/openapitools/jackson/nullable/JsonNullableJackson2Deserializer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import java.io.IOException;
1616

17-
public class JsonNullableDeserializer extends ReferenceTypeDeserializer<JsonNullable<Object>> {
17+
public class JsonNullableJackson2Deserializer extends ReferenceTypeDeserializer<JsonNullable<Object>> {
1818

1919
private static final long serialVersionUID = 1L;
2020

@@ -25,8 +25,8 @@ public class JsonNullableDeserializer extends ReferenceTypeDeserializer<JsonNull
2525
/* Life-cycle
2626
/**********************************************************
2727
*/
28-
public JsonNullableDeserializer(JavaType fullType, ValueInstantiator inst,
29-
TypeDeserializer typeDeser, JsonDeserializer<?> deser) {
28+
public JsonNullableJackson2Deserializer(JavaType fullType, ValueInstantiator inst,
29+
TypeDeserializer typeDeser, JsonDeserializer<?> deser) {
3030
super(fullType, inst, typeDeser, deser);
3131
if (fullType instanceof ReferenceType && ((ReferenceType) fullType).getReferencedType() != null) {
3232
this.isStringDeserializer = ((ReferenceType) fullType).getReferencedType().isTypeOrSubTypeOf(String.class);
@@ -52,8 +52,8 @@ public JsonNullable<Object> deserialize(JsonParser p, DeserializationContext ctx
5252
}
5353

5454
@Override
55-
public JsonNullableDeserializer withResolved(TypeDeserializer typeDeser, JsonDeserializer<?> valueDeser) {
56-
return new JsonNullableDeserializer(_fullType, _valueInstantiator,
55+
public JsonNullableJackson2Deserializer withResolved(TypeDeserializer typeDeser, JsonDeserializer<?> valueDeser) {
56+
return new JsonNullableJackson2Deserializer(_fullType, _valueInstantiator,
5757
typeDeser, valueDeser);
5858
}
5959

src/main/java/org/openapitools/jackson/nullable/JsonNullableDeserializers.java renamed to src/main/java/org/openapitools/jackson/nullable/JsonNullableJackson2Deserializers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
88
import com.fasterxml.jackson.databind.type.ReferenceType;
99

10-
public class JsonNullableDeserializers extends Deserializers.Base {
10+
public class JsonNullableJackson2Deserializers extends Deserializers.Base {
1111

1212
@Override
1313
public JsonDeserializer<?> findReferenceDeserializer(ReferenceType refType,
1414
DeserializationConfig config, BeanDescription beanDesc,
1515
TypeDeserializer contentTypeDeserializer, JsonDeserializer<?> contentDeserializer) {
16-
return (refType.hasRawClass(JsonNullable.class)) ? new JsonNullableDeserializer(refType, null, contentTypeDeserializer,contentDeserializer) : null;
16+
return (refType.hasRawClass(JsonNullable.class)) ? new JsonNullableJackson2Deserializer(refType, null, contentTypeDeserializer,contentDeserializer) : null;
1717
}
1818
}

src/main/java/org/openapitools/jackson/nullable/JsonNullableSerializer.java renamed to src/main/java/org/openapitools/jackson/nullable/JsonNullableJackson2Serializer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.fasterxml.jackson.databind.type.ReferenceType;
88
import com.fasterxml.jackson.databind.util.NameTransformer;
99

10-
public class JsonNullableSerializer extends ReferenceTypeSerializer<JsonNullable<?>> {
10+
public class JsonNullableJackson2Serializer extends ReferenceTypeSerializer<JsonNullable<?>> {
1111

1212
private static final long serialVersionUID = 1L;
1313

@@ -17,14 +17,14 @@ public class JsonNullableSerializer extends ReferenceTypeSerializer<JsonNullable
1717
/**********************************************************
1818
*/
1919

20-
protected JsonNullableSerializer(ReferenceType fullType, boolean staticTyping,
21-
TypeSerializer vts, JsonSerializer<Object> ser) {
20+
protected JsonNullableJackson2Serializer(ReferenceType fullType, boolean staticTyping,
21+
TypeSerializer vts, JsonSerializer<Object> ser) {
2222
super(fullType, staticTyping, vts, ser);
2323
}
2424

25-
protected JsonNullableSerializer(JsonNullableSerializer base, BeanProperty property,
26-
TypeSerializer vts, JsonSerializer<?> valueSer, NameTransformer unwrapper,
27-
Object suppressableValue)
25+
protected JsonNullableJackson2Serializer(JsonNullableJackson2Serializer base, BeanProperty property,
26+
TypeSerializer vts, JsonSerializer<?> valueSer, NameTransformer unwrapper,
27+
Object suppressableValue)
2828
{
2929
// Keep suppressNulls to false to always serialize JsonNullable[null]
3030
super(base, property, vts, valueSer, unwrapper,
@@ -36,15 +36,15 @@ protected ReferenceTypeSerializer<JsonNullable<?>> withResolved(BeanProperty pro
3636
TypeSerializer vts, JsonSerializer<?> valueSer,
3737
NameTransformer unwrapper)
3838
{
39-
return new JsonNullableSerializer(this, prop, vts, valueSer, unwrapper,
39+
return new JsonNullableJackson2Serializer(this, prop, vts, valueSer, unwrapper,
4040
_suppressableValue);
4141
}
4242

4343
@Override
4444
public ReferenceTypeSerializer<JsonNullable<?>> withContentInclusion(Object suppressableValue,
4545
boolean suppressNulls)
4646
{
47-
return new JsonNullableSerializer(this, _property, _valueTypeSerializer,
47+
return new JsonNullableJackson2Serializer(this, _property, _valueTypeSerializer,
4848
_valueSerializer, _unwrapper,
4949
suppressableValue);
5050
}

src/main/java/org/openapitools/jackson/nullable/JsonNullableSerializers.java renamed to src/main/java/org/openapitools/jackson/nullable/JsonNullableJackson2Serializers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.fasterxml.jackson.databind.ser.Serializers;
99
import com.fasterxml.jackson.databind.type.ReferenceType;
1010

11-
public class JsonNullableSerializers extends Serializers.Base {
11+
public class JsonNullableJackson2Serializers extends Serializers.Base {
1212

1313
@Override
1414
public JsonSerializer<?> findReferenceSerializer(SerializationConfig config,
@@ -17,7 +17,7 @@ public JsonSerializer<?> findReferenceSerializer(SerializationConfig config,
1717
if (JsonNullable.class.isAssignableFrom(refType.getRawClass())) {
1818
boolean staticTyping = (contentTypeSerializer == null)
1919
&& config.isEnabled(MapperFeature.USE_STATIC_TYPING);
20-
return new JsonNullableSerializer(refType, staticTyping,
20+
return new JsonNullableJackson2Serializer(refType, staticTyping,
2121
contentTypeSerializer, contentValueSerializer);
2222
}
2323
return null;

src/main/java/org/openapitools/jackson/nullable/JsonNullableTypeModifier.java renamed to src/main/java/org/openapitools/jackson/nullable/JsonNullableJackson2TypeModifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import java.lang.reflect.Type;
1010

11-
public class JsonNullableTypeModifier extends TypeModifier {
11+
public class JsonNullableJackson2TypeModifier extends TypeModifier {
1212

1313
@Override
1414
public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, TypeFactory typeFactory)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.openapitools.jackson.nullable;
2+
3+
import tools.jackson.core.JsonGenerator;
4+
import tools.jackson.databind.PropertyName;
5+
import tools.jackson.databind.SerializationContext;
6+
import tools.jackson.databind.ser.BeanPropertyWriter;
7+
import tools.jackson.databind.util.NameTransformer;
8+
9+
public class JsonNullableJackson3BeanPropertyWriter extends BeanPropertyWriter {
10+
11+
protected JsonNullableJackson3BeanPropertyWriter(BeanPropertyWriter base) {
12+
super(base);
13+
}
14+
15+
protected JsonNullableJackson3BeanPropertyWriter(BeanPropertyWriter base, PropertyName newName) {
16+
super(base, newName);
17+
}
18+
19+
@Override
20+
protected BeanPropertyWriter _new(PropertyName newName) {
21+
return new JsonNullableJackson3BeanPropertyWriter(this, newName);
22+
}
23+
24+
@Override
25+
public BeanPropertyWriter unwrappingWriter(NameTransformer unwrapper) {
26+
return new UnwrappingJsonNullableJackson3BeanPropertyWriter(this, unwrapper);
27+
}
28+
29+
@Override
30+
public void serializeAsProperty(Object bean, JsonGenerator jgen, SerializationContext ctxt) throws Exception {
31+
Object value = get(bean);
32+
if (JsonNullable.undefined().equals(value) || (_nullSerializer == null && value == null)) {
33+
return;
34+
}
35+
super.serializeAsProperty(bean, jgen, ctxt);
36+
}
37+
38+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.openapitools.jackson.nullable;
2+
3+
4+
import tools.jackson.core.JacksonException;
5+
import tools.jackson.core.JsonParser;
6+
import tools.jackson.core.JsonToken;
7+
import tools.jackson.databind.DeserializationConfig;
8+
import tools.jackson.databind.DeserializationContext;
9+
import tools.jackson.databind.JavaType;
10+
import tools.jackson.databind.ValueDeserializer;
11+
import tools.jackson.databind.deser.ValueInstantiator;
12+
import tools.jackson.databind.deser.std.ReferenceTypeDeserializer;
13+
import tools.jackson.databind.jsontype.TypeDeserializer;
14+
import tools.jackson.databind.type.ReferenceType;
15+
16+
public class JsonNullableJackson3Deserializer extends ReferenceTypeDeserializer<JsonNullable<Object>> {
17+
18+
19+
private boolean isStringDeserializer = false;
20+
21+
/*
22+
/**********************************************************
23+
/* Life-cycle
24+
/**********************************************************
25+
*/
26+
public JsonNullableJackson3Deserializer(JavaType fullType, ValueInstantiator inst,
27+
TypeDeserializer typeDeser, ValueDeserializer<?> deser) {
28+
super(fullType, inst, typeDeser, deser);
29+
if (fullType instanceof ReferenceType && ((ReferenceType) fullType).getReferencedType() != null) {
30+
this.isStringDeserializer = ((ReferenceType) fullType).getReferencedType().isTypeOrSubTypeOf(String.class);
31+
}
32+
}
33+
34+
/*
35+
/**********************************************************
36+
/* Abstract method implementations
37+
/**********************************************************
38+
*/
39+
40+
@Override
41+
public JsonNullable<Object> deserialize(JsonParser p, DeserializationContext ctxt) throws JacksonException {
42+
JsonToken t = p.currentToken();
43+
if (t == JsonToken.VALUE_STRING && !isStringDeserializer) {
44+
String str = p.getString().trim();
45+
if (str.isEmpty()) {
46+
return JsonNullable.undefined();
47+
}
48+
}
49+
return super.deserialize(p, ctxt);
50+
}
51+
52+
@Override
53+
protected ReferenceTypeDeserializer<JsonNullable<Object>> withResolved(TypeDeserializer typeDeser, ValueDeserializer<?> valueDeser) {
54+
return new JsonNullableJackson3Deserializer(_fullType, _valueInstantiator,
55+
typeDeser, valueDeser);
56+
}
57+
58+
@Override
59+
public Object getAbsentValue(DeserializationContext ctxt) {
60+
return JsonNullable.undefined();
61+
}
62+
63+
@Override
64+
public JsonNullable<Object> getNullValue(DeserializationContext ctxt) {
65+
return JsonNullable.of(null);
66+
}
67+
68+
@Override
69+
public Object getEmptyValue(DeserializationContext ctxt) {
70+
return JsonNullable.undefined();
71+
}
72+
73+
@Override
74+
public JsonNullable<Object> referenceValue(Object contents) {
75+
return JsonNullable.of(contents);
76+
}
77+
78+
@Override
79+
public Object getReferenced(JsonNullable<Object> reference) {
80+
return reference.get();
81+
}
82+
83+
@Override
84+
public JsonNullable<Object> updateReference(JsonNullable<Object> reference, Object contents) {
85+
return JsonNullable.of(contents);
86+
}
87+
88+
@Override
89+
public Boolean supportsUpdate(DeserializationConfig config) {
90+
// yes; regardless of value deserializer reference itself may be updated
91+
return Boolean.TRUE;
92+
}
93+
}

0 commit comments

Comments
 (0)