Skip to content

Commit 223ecbe

Browse files
committed
Fix #192
1 parent df0e7cb commit 223ecbe

File tree

8 files changed

+124
-58
lines changed

8 files changed

+124
-58
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
package tools.jackson.datatype.hibernate5.jakarta.tofix;
1+
package tools.jackson.datatype.hibernate5.jakarta;
22

33
import java.util.Set;
44

5-
import org.hibernate.Hibernate;
5+
import org.junit.jupiter.api.*;
66

77
import tools.jackson.databind.DatabindException;
88
import tools.jackson.databind.DefaultTyping;
99
import tools.jackson.databind.ObjectMapper;
1010
import tools.jackson.databind.json.JsonMapper;
11-
import tools.jackson.databind.jsontype.DefaultBaseTypeLimitingValidator;
1211
import tools.jackson.datatype.hibernate5.jakarta.BaseTest;
1312
import tools.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule;
1413
import tools.jackson.datatype.hibernate5.jakarta.data.Customer;
1514
import tools.jackson.datatype.hibernate5.jakarta.data.Payment;
16-
15+
import tools.jackson.datatype.hibernate5.jakarta.testutil.NoCheckSubTypeValidator;
1716
import jakarta.persistence.EntityManager;
1817
import jakarta.persistence.EntityManagerFactory;
1918
import jakarta.persistence.Persistence;
2019

21-
import org.junit.jupiter.api.*;
20+
import org.hibernate.Hibernate;
2221

2322
import static org.junit.jupiter.api.Assertions.*;
2423

@@ -41,7 +40,6 @@ public void tearDown() throws Exception {
4140
}
4241

4342
// [datatypes-hibernate#93], backwards compatible case
44-
@Disabled // https://github.com/FasterXML/jackson-datatype-hibernate/issues/192
4543
@Test
4644
public void testNoReplacePersistentCollection() throws Exception {
4745
final ObjectMapper mapper = hibernateMapper(new Hibernate5JakartaModule()
@@ -93,7 +91,7 @@ public void testReplacePersistentCollection() throws Exception {
9391
private ObjectMapper hibernateMapper(Hibernate5JakartaModule module) {
9492
return JsonMapper.builder()
9593
.addModule(module)
96-
.activateDefaultTyping(new DefaultBaseTypeLimitingValidator(),
94+
.activateDefaultTyping(new NoCheckSubTypeValidator(),
9795
DefaultTyping.NON_FINAL)
9896
.build();
9997
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tools.jackson.datatype.hibernate5.jakarta.testutil;
2+
3+
import tools.jackson.databind.DatabindContext;
4+
import tools.jackson.databind.JavaType;
5+
import tools.jackson.databind.jsontype.PolymorphicTypeValidator;
6+
7+
/**
8+
* Test-only {@link PolymorphicTypeValidator} used by tests that should not block
9+
* use of any subtypes.
10+
*/
11+
public final class NoCheckSubTypeValidator
12+
extends PolymorphicTypeValidator.Base
13+
{
14+
private static final long serialVersionUID = 3L;
15+
16+
public final static NoCheckSubTypeValidator instance = new NoCheckSubTypeValidator();
17+
18+
@Override
19+
public Validity validateBaseType(DatabindContext ctxt, JavaType baseType) {
20+
return Validity.INDETERMINATE;
21+
}
22+
23+
@Override
24+
public Validity validateSubClassName(DatabindContext ctxt,
25+
JavaType baseType, String subClassName) {
26+
return Validity.ALLOWED;
27+
}
28+
29+
@Override
30+
public Validity validateSubType(DatabindContext ctxt, JavaType baseType,
31+
JavaType subType) {
32+
return Validity.ALLOWED;
33+
}
34+
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package tools.jackson.datatype.hibernate5.tofix;
1+
package tools.jackson.datatype.hibernate5;
22

33
import java.util.*;
44

5+
import org.junit.jupiter.api.*;
6+
57
import tools.jackson.databind.DatabindException;
68
import tools.jackson.databind.DefaultTyping;
79
import tools.jackson.databind.ObjectMapper;
810
import tools.jackson.databind.json.JsonMapper;
9-
import tools.jackson.databind.jsontype.DefaultBaseTypeLimitingValidator;
10-
import tools.jackson.datatype.hibernate5.BaseTest;
11-
import tools.jackson.datatype.hibernate5.Hibernate5Module;
11+
1212
import tools.jackson.datatype.hibernate5.data.Customer;
1313
import tools.jackson.datatype.hibernate5.data.Payment;
14-
import org.hibernate.Hibernate;
14+
import tools.jackson.datatype.hibernate5.testutil.NoCheckSubTypeValidator;
1515

16-
import org.junit.jupiter.api.*;
16+
import org.hibernate.Hibernate;
1717

1818
import javax.persistence.EntityManager;
1919
import javax.persistence.EntityManagerFactory;
@@ -40,7 +40,6 @@ public void tearDown() throws Exception {
4040
}
4141

4242
// [datatypes-hibernate#93], backwards compatible case
43-
@Disabled // https://github.com/FasterXML/jackson-datatype-hibernate/issues/192
4443
@Test
4544
public void testNoReplacePersistentCollection() throws Exception {
4645
final ObjectMapper mapper = hibernateMapper(new Hibernate5Module()
@@ -95,7 +94,7 @@ public void testReplacePersistentCollection() throws Exception {
9594
private ObjectMapper hibernateMapper(Hibernate5Module module) {
9695
return JsonMapper.builder()
9796
.addModule(module)
98-
.activateDefaultTyping(new DefaultBaseTypeLimitingValidator(),
97+
.activateDefaultTyping(new NoCheckSubTypeValidator(),
9998
DefaultTyping.NON_FINAL)
10099
.build();
101100
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tools.jackson.datatype.hibernate5.testutil;
2+
3+
import tools.jackson.databind.DatabindContext;
4+
import tools.jackson.databind.JavaType;
5+
import tools.jackson.databind.jsontype.PolymorphicTypeValidator;
6+
7+
/**
8+
* Test-only {@link PolymorphicTypeValidator} used by tests that should not block
9+
* use of any subtypes.
10+
*/
11+
public final class NoCheckSubTypeValidator
12+
extends PolymorphicTypeValidator.Base
13+
{
14+
private static final long serialVersionUID = 3L;
15+
16+
public final static NoCheckSubTypeValidator instance = new NoCheckSubTypeValidator();
17+
18+
@Override
19+
public Validity validateBaseType(DatabindContext ctxt, JavaType baseType) {
20+
return Validity.INDETERMINATE;
21+
}
22+
23+
@Override
24+
public Validity validateSubClassName(DatabindContext ctxt,
25+
JavaType baseType, String subClassName) {
26+
return Validity.ALLOWED;
27+
}
28+
29+
@Override
30+
public Validity validateSubType(DatabindContext ctxt, JavaType baseType,
31+
JavaType subType) {
32+
return Validity.ALLOWED;
33+
}
34+
}

hibernate6/src/test/java/tools/jackson/datatype/hibernate6/ReplacePersistentCollectionTest.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public void tearDown() throws Exception {
4040
emf.close();
4141
}
4242

43-
// [Issue#93], backwards compatible case
43+
// [datatypes-hibernate#93], backwards compatible case
4444
@Test
4545
public void testNoReplacePersistentCollection() throws Exception {
46-
final ObjectMapper mapper = hibernateMapper(new Hibernate6Module()
47-
.configure(Hibernate6Module.Feature.FORCE_LAZY_LOADING, true)
48-
);
46+
final ObjectMapper mapper = hibernateMapper(new Hibernate6Module()
47+
.configure(Hibernate6Module.Feature.FORCE_LAZY_LOADING, true)
48+
);
4949

5050
Customer customer = em.find(Customer.class, 103);
5151
assertFalse(Hibernate.isInitialized(customer.getPayments()));
@@ -63,24 +63,24 @@ public void testNoReplacePersistentCollection() throws Exception {
6363
}
6464
}
6565

66-
// [Issue#93], backwards compatible case
66+
// [datatypes-hibernate#93], backwards compatible case
6767
@Test
6868
public void testReplacePersistentCollection() throws Exception {
69-
final ObjectMapper mapper = hibernateMapper(new Hibernate6Module()
70-
.configure(Hibernate6Module.Feature.FORCE_LAZY_LOADING, true)
71-
.configure(Hibernate6Module.Feature.REPLACE_PERSISTENT_COLLECTIONS, true)
72-
);
73-
74-
Customer customer = em.find(Customer.class, 103);
75-
assertFalse(Hibernate.isInitialized(customer.getPayments()));
76-
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
77-
assertFalse(json.contains("org.hibernate.collection"));
78-
// should force loading...
79-
Set<Payment> payments = customer.getPayments();
80-
81-
assertTrue(Hibernate.isInitialized(payments));
82-
Customer stuff = mapper.readValue(json, Customer.class);
83-
assertNotNull(stuff);
69+
final ObjectMapper mapper = hibernateMapper(new Hibernate6Module()
70+
.configure(Hibernate6Module.Feature.FORCE_LAZY_LOADING, true)
71+
.configure(Hibernate6Module.Feature.REPLACE_PERSISTENT_COLLECTIONS, true)
72+
);
73+
74+
Customer customer = em.find(Customer.class, 103);
75+
assertFalse(Hibernate.isInitialized(customer.getPayments()));
76+
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
77+
assertFalse(json.contains("org.hibernate.collection"));
78+
// should force loading...
79+
Set<Payment> payments = customer.getPayments();
80+
81+
assertTrue(Hibernate.isInitialized(payments));
82+
Customer stuff = mapper.readValue(json, Customer.class);
83+
assertNotNull(stuff);
8484

8585
// Map<?, ?> stuff = mapper.readValue(json, Map.class);
8686
//
@@ -92,7 +92,8 @@ public void testReplacePersistentCollection() throws Exception {
9292
private ObjectMapper hibernateMapper(Hibernate6Module module) {
9393
return JsonMapper.builder()
9494
.addModule(module)
95-
.activateDefaultTyping(new DefaultBaseTypeLimitingValidator(), DefaultTyping.NON_FINAL)
95+
.activateDefaultTyping(new DefaultBaseTypeLimitingValidator(),
96+
DefaultTyping.NON_FINAL)
9697
.build();
9798
}
9899
}

hibernate7/src/main/java/tools/jackson/datatype/hibernate7/Hibernate7SerializerModifier.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
public class Hibernate7SerializerModifier
1212
extends ValueSerializerModifier
1313
{
14+
private static final long serialVersionUID = 3L;
15+
1416
protected final int _features;
1517

1618
protected final SessionFactory _sessionFactory;

hibernate7/src/main/java/tools/jackson/datatype/hibernate7/PersistentCollectionSerializer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
public class PersistentCollectionSerializer
3737
extends StdContainerSerializer<Object>
3838
{
39-
private static final long serialVersionUID = 1L;
40-
4139
/**
4240
* Type for which underlying serializer was created.
4341
*/

hibernate7/src/test/java/tools/jackson/datatype/hibernate7/ReplacePersistentCollectionTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public void tearDown() throws Exception {
3939
emf.close();
4040
}
4141

42-
// [Issue#93], backwards compatible case
42+
// [datatypes-hibernate#93], backwards compatible case
4343
@Test
4444
public void testNoReplacePersistentCollection() throws Exception {
45-
final ObjectMapper mapper = hibernateMapper(new Hibernate7Module()
46-
.configure(Hibernate7Module.Feature.FORCE_LAZY_LOADING, true)
47-
);
45+
final ObjectMapper mapper = hibernateMapper(new Hibernate7Module()
46+
.configure(Hibernate7Module.Feature.FORCE_LAZY_LOADING, true)
47+
);
4848

4949
Customer customer = em.find(Customer.class, 103);
5050
assertFalse(Hibernate.isInitialized(customer.getPayments()));
@@ -62,24 +62,24 @@ public void testNoReplacePersistentCollection() throws Exception {
6262
}
6363
}
6464

65-
// [Issue#93], backwards compatible case
65+
// [datatypes-hibernate#93], backwards compatible case
6666
@Test
6767
public void testReplacePersistentCollection() throws Exception {
68-
final ObjectMapper mapper = hibernateMapper(new Hibernate7Module()
69-
.configure(Hibernate7Module.Feature.FORCE_LAZY_LOADING, true)
70-
.configure(Hibernate7Module.Feature.REPLACE_PERSISTENT_COLLECTIONS, true)
71-
);
72-
73-
Customer customer = em.find(Customer.class, 103);
74-
assertFalse(Hibernate.isInitialized(customer.getPayments()));
75-
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
76-
assertFalse(json.contains("org.hibernate.collection"));
77-
// should force loading...
78-
Set<Payment> payments = customer.getPayments();
79-
80-
assertTrue(Hibernate.isInitialized(payments));
81-
Customer stuff = mapper.readValue(json, Customer.class);
82-
assertNotNull(stuff);
68+
final ObjectMapper mapper = hibernateMapper(new Hibernate7Module()
69+
.configure(Hibernate7Module.Feature.FORCE_LAZY_LOADING, true)
70+
.configure(Hibernate7Module.Feature.REPLACE_PERSISTENT_COLLECTIONS, true)
71+
);
72+
73+
Customer customer = em.find(Customer.class, 103);
74+
assertFalse(Hibernate.isInitialized(customer.getPayments()));
75+
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
76+
assertFalse(json.contains("org.hibernate.collection"));
77+
// should force loading...
78+
Set<Payment> payments = customer.getPayments();
79+
80+
assertTrue(Hibernate.isInitialized(payments));
81+
Customer stuff = mapper.readValue(json, Customer.class);
82+
assertNotNull(stuff);
8383

8484
// Map<?, ?> stuff = mapper.readValue(json, Map.class);
8585
//

0 commit comments

Comments
 (0)