Skip to content

Commit dd42908

Browse files
committed
HV-2161 Rework documentation example for Jackson 3
This use case: https://docs.hibernate.org/stable/validator/reference/en-US/html_single/#section-property-node-name-provider Uses the interpolator, which is an internal component of Jackson that was exposed for testing purposes. In Jackson 3, the interpolator is no longer publicly exposed, a Visitor must be used instead. See discussion here: FasterXML/jackson#330
1 parent 44382bd commit dd42908

File tree

5 files changed

+65
-38
lines changed

5 files changed

+65
-38
lines changed

documentation/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<scope>test</scope>
106106
</dependency>
107107
<dependency>
108-
<groupId>com.fasterxml.jackson.core</groupId>
108+
<groupId>tools.jackson.core</groupId>
109109
<artifactId>jackson-databind</artifactId>
110110
<scope>test</scope>
111111
</dependency>

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter12/nodenameprovider/JacksonPropertyNodeNameProvider.java

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,75 @@
44
*/
55
package org.hibernate.validator.referenceguide.chapter12.nodenameprovider;
66

7-
//tag::include[]
87
import org.hibernate.validator.spi.nodenameprovider.JavaBeanProperty;
98
import org.hibernate.validator.spi.nodenameprovider.Property;
109
import org.hibernate.validator.spi.nodenameprovider.PropertyNodeNameProvider;
1110

12-
import com.fasterxml.jackson.databind.BeanDescription;
13-
import com.fasterxml.jackson.databind.JavaType;
14-
import com.fasterxml.jackson.databind.ObjectMapper;
15-
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
11+
import tools.jackson.core.JacksonException;
12+
import tools.jackson.databind.BeanProperty;
13+
import tools.jackson.databind.JavaType;
14+
import tools.jackson.databind.ObjectMapper;
15+
import tools.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
16+
import tools.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
17+
18+
//tag::include[]
1619

1720
public class JacksonPropertyNodeNameProvider implements PropertyNodeNameProvider {
1821
private final ObjectMapper objectMapper = new ObjectMapper();
1922

2023
@Override
2124
public String getName(Property property) {
22-
if ( property instanceof JavaBeanProperty ) {
23-
return getJavaBeanPropertyName( (JavaBeanProperty) property );
25+
if (property instanceof JavaBeanProperty javaBeanProperty) {
26+
var visitor = new JsonPropertyNameGetter(javaBeanProperty.getMemberName());
27+
try {
28+
objectMapper.acceptJsonFormatVisitor(
29+
javaBeanProperty.getDeclaringClass(),
30+
visitor
31+
);
32+
var attributeName = visitor.getAttributeName();
33+
return attributeName != null ? attributeName : property.getName();
34+
} catch (JacksonException ignored) {}
2435
}
25-
26-
return getDefaultName( property );
36+
return property.getName();
2737
}
2838

29-
private String getJavaBeanPropertyName(JavaBeanProperty property) {
30-
JavaType type = objectMapper.constructType( property.getDeclaringClass() );
31-
BeanDescription desc = objectMapper.getSerializationConfig().introspect( type );
39+
// Visitor wrapper
40+
private static class JsonPropertyNameGetter extends JsonFormatVisitorWrapper.Base {
3241

33-
return desc.findProperties()
34-
.stream()
35-
.filter( prop -> prop.getInternalName().equals( property.getName() ) )
36-
.map( BeanPropertyDefinition::getName )
37-
.findFirst()
38-
.orElse( property.getName() );
39-
}
42+
private final JsonObjectFormatVisitor visitor;
43+
private String attributeName = null;
44+
45+
JsonPropertyNameGetter(String memberName) {
46+
this.visitor = new JsonObjectFormatVisitor.Base() {
47+
48+
@Override
49+
public void property(BeanProperty prop) {
50+
setAttributeName(prop);
51+
}
52+
53+
@Override
54+
public void optionalProperty(BeanProperty prop) {
55+
setAttributeName(prop);
56+
}
57+
58+
private void setAttributeName(BeanProperty writer) {
59+
if (memberName.equalsIgnoreCase(writer.getMember().getName())) {
60+
attributeName = writer.getName();
61+
}
62+
}
63+
};
64+
}
65+
66+
public String getAttributeName() {
67+
return attributeName;
68+
}
69+
70+
@Override
71+
public JsonObjectFormatVisitor expectObjectFormat(JavaType type) {
72+
return visitor;
73+
}
4074

41-
private String getDefaultName(Property property) {
42-
return property.getName();
4375
}
76+
4477
}
4578
//end::include[]

documentation/src/test/java/org/hibernate/validator/referenceguide/chapter12/nodenameprovider/PersonSerializationTest.java

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

99
import org.junit.Test;
1010

11-
import com.fasterxml.jackson.core.JsonProcessingException;
12-
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import tools.jackson.core.JacksonException;
12+
import tools.jackson.databind.ObjectMapper;
1313

1414
//tag::include[]
1515
public class PersonSerializationTest {
1616
private final ObjectMapper objectMapper = new ObjectMapper();
1717

1818
@Test
19-
public void personIsSerialized() throws JsonProcessingException {
19+
public void personIsSerialized() throws JacksonException {
2020
Person person = new Person( "Clark", "Kent" );
2121

2222
String serializedPerson = objectMapper.writeValueAsString( person );

engine/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
<scope>test</scope>
178178
</dependency>
179179
<dependency>
180-
<groupId>com.fasterxml.jackson.core</groupId>
180+
<groupId>tools.jackson.core</groupId>
181181
<artifactId>jackson-databind</artifactId>
182182
<scope>test</scope>
183183
</dependency>

pom.xml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@
201201
<version.com.google.guava>33.5.0-jre</version.com.google.guava>
202202
<version.org.springframework.spring-expression>7.0.2</version.org.springframework.spring-expression>
203203
<version.org.jboss.arquillian.container.arquillian-weld-embedded>4.0.0.Final</version.org.jboss.arquillian.container.arquillian-weld-embedded>
204-
<version.com.fasterxml.jackson.core.jackson-databind>2.20.1</version.com.fasterxml.jackson.core.jackson-databind>
205-
<version.com.fasterxml.jackson.core.jackson-annotations>2.20</version.com.fasterxml.jackson.core.jackson-annotations>
204+
<version.tools.jackson.core.jackson-bom>3.0.3</version.tools.jackson.core.jackson-bom>
206205
<version.net.bytebuddy.byte-buddy>1.18.2</version.net.bytebuddy.byte-buddy>
207206

208207
<!-- OSGi dependencies -->
@@ -644,16 +643,11 @@
644643
<scope>test</scope>
645644
</dependency>
646645
<dependency>
647-
<groupId>com.fasterxml.jackson.core</groupId>
648-
<artifactId>jackson-databind</artifactId>
649-
<version>${version.com.fasterxml.jackson.core.jackson-databind}</version>
650-
<scope>test</scope>
651-
</dependency>
652-
<dependency>
653-
<groupId>com.fasterxml.jackson.core</groupId>
654-
<artifactId>jackson-annotations</artifactId>
655-
<version>${version.com.fasterxml.jackson.core.jackson-annotations}</version>
656-
<scope>test</scope>
646+
<groupId>tools.jackson</groupId>
647+
<artifactId>jackson-bom</artifactId>
648+
<type>pom</type>
649+
<scope>import</scope>
650+
<version>${version.tools.jackson.core.jackson-bom}</version>
657651
</dependency>
658652
<dependency>
659653
<groupId>javax.inject</groupId>

0 commit comments

Comments
 (0)