Skip to content

Commit 5538fba

Browse files
committed
Merge pull request #9443 from tsachev:gh-9442
* pr/9443: Polish Polish "Allow abstract serializers/deserializer in @JsonComponent" Allow abstract serializers/deserializer in @JsonComponent
2 parents acda4f9 + 31ee45b commit 5538fba

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

spring-boot/src/main/java/org/springframework/boot/jackson/JsonComponentModule.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.jackson;
1818

19+
import java.lang.reflect.Modifier;
1920
import java.util.Map;
2021

2122
import javax.annotation.PostConstruct;
@@ -78,8 +79,9 @@ private void addJsonBean(Object bean) {
7879
addDeserializerWithDeducedType((JsonDeserializer<?>) bean);
7980
}
8081
for (Class<?> innerClass : bean.getClass().getDeclaredClasses()) {
81-
if (JsonSerializer.class.isAssignableFrom(innerClass)
82-
|| JsonDeserializer.class.isAssignableFrom(innerClass)) {
82+
if (!Modifier.isAbstract(innerClass.getModifiers()) &&
83+
(JsonSerializer.class.isAssignableFrom(innerClass)
84+
|| JsonDeserializer.class.isAssignableFrom(innerClass))) {
8385
try {
8486
addJsonBean(innerClass.newInstance());
8587
}

spring-boot/src/test/java/org/springframework/boot/jackson/JsonComponentModuleTests.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import com.fasterxml.jackson.databind.Module;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import org.junit.After;
2122
import org.junit.Test;
2223

2324
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -31,34 +32,54 @@
3132
*/
3233
public class JsonComponentModuleTests {
3334

35+
private AnnotationConfigApplicationContext context;
36+
37+
@After
38+
public void closeContext() {
39+
if (this.context != null) {
40+
this.context.close();
41+
}
42+
}
43+
3444
@Test
3545
public void moduleShouldRegisterSerializers() throws Exception {
36-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
37-
JsonComponentModule.class, OnlySerializer.class);
38-
JsonComponentModule module = context.getBean(JsonComponentModule.class);
46+
load(OnlySerializer.class);
47+
JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
3948
assertSerialize(module);
40-
context.close();
4149
}
4250

4351
@Test
4452
public void moduleShouldRegisterDeserializers() throws Exception {
45-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
46-
JsonComponentModule.class, OnlyDeserializer.class);
47-
JsonComponentModule module = context.getBean(JsonComponentModule.class);
53+
load(OnlyDeserializer.class);
54+
JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
4855
assertDeserialize(module);
49-
context.close();
5056
}
5157

5258
@Test
5359
public void moduleShouldRegisterInnerClasses() throws Exception {
60+
load(NameAndAgeJsonComponent.class);
61+
JsonComponentModule module = this.context.getBean(JsonComponentModule.class);
62+
assertSerialize(module);
63+
assertDeserialize(module);
64+
}
65+
66+
@Test
67+
public void moduleShouldAllowInnerAbstractClasses() throws Exception {
5468
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
55-
JsonComponentModule.class, NameAndAgeJsonComponent.class);
69+
JsonComponentModule.class, ComponentWithInnerAbstractClass.class);
5670
JsonComponentModule module = context.getBean(JsonComponentModule.class);
5771
assertSerialize(module);
58-
assertDeserialize(module);
5972
context.close();
6073
}
6174

75+
private void load(Class<?>... configs) {
76+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
77+
ctx.register(configs);
78+
ctx.register(JsonComponentModule.class);
79+
ctx.refresh();
80+
this.context = ctx;
81+
}
82+
6283
private void assertSerialize(Module module) throws Exception {
6384
ObjectMapper mapper = new ObjectMapper();
6485
mapper.registerModule(module);
@@ -85,4 +106,15 @@ static class OnlyDeserializer extends NameAndAgeJsonComponent.Deserializer {
85106

86107
}
87108

109+
@JsonComponent
110+
static class ComponentWithInnerAbstractClass {
111+
112+
private static abstract class AbstractSerializer extends NameAndAgeJsonComponent.Serializer {
113+
114+
}
115+
116+
static class ConcreteSerializer extends AbstractSerializer {
117+
118+
}
119+
}
88120
}

0 commit comments

Comments
 (0)