Skip to content

Commit 39ec4dd

Browse files
committed
Simplify registration of Jackson mixin types
Closes gh-25920
1 parent 6f8ce3d commit 39ec4dd

File tree

12 files changed

+853
-1
lines changed

12 files changed

+853
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@
4141
import org.springframework.beans.factory.BeanFactoryUtils;
4242
import org.springframework.beans.factory.ListableBeanFactory;
4343
import org.springframework.boot.autoconfigure.AutoConfiguration;
44+
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
4445
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4546
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4647
import org.springframework.boot.autoconfigure.jackson.JacksonProperties.ConstructorDetectorStrategy;
4748
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4849
import org.springframework.boot.jackson.JsonComponentModule;
50+
import org.springframework.boot.jackson.JsonMixinModule;
51+
import org.springframework.boot.jackson.JsonMixinScanPackages;
4952
import org.springframework.context.ApplicationContext;
5053
import org.springframework.context.annotation.Bean;
5154
import org.springframework.context.annotation.Configuration;
@@ -93,6 +96,15 @@ public JsonComponentModule jsonComponentModule() {
9396
return new JsonComponentModule();
9497
}
9598

99+
@Bean
100+
public JsonMixinModule jsonMixinModule(ApplicationContext context) {
101+
List<String> packages = JsonMixinScanPackages.get(context).getPackageNames();
102+
if (packages.isEmpty() && AutoConfigurationPackages.has(context)) {
103+
packages = AutoConfigurationPackages.get(context);
104+
}
105+
return new JsonMixinModule(context, packages);
106+
}
107+
96108
@Configuration(proxyBeanMethods = false)
97109
@ConditionalOnClass(Jackson2ObjectMapperBuilder.class)
98110
static class JacksonObjectMapperConfiguration {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -52,6 +52,7 @@
5252
import org.springframework.boot.autoconfigure.AutoConfigurations;
5353
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
5454
import org.springframework.boot.jackson.JsonComponent;
55+
import org.springframework.boot.jackson.JsonMixinModule;
5556
import org.springframework.boot.jackson.JsonObjectSerializer;
5657
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
5758
import org.springframework.context.annotation.Bean;
@@ -90,6 +91,11 @@ void doubleModuleRegistration() {
9091
});
9192
}
9293

94+
@Test
95+
void jsonMixinModuleShouldBeAutoconfigured() {
96+
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(JsonMixinModule.class));
97+
}
98+
9399
@Test
94100
void noCustomDateFormat() {
95101
this.contextRunner.run((context) -> {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jackson;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
/**
28+
* Provides a mixin class implementation that registers with Jackson when using
29+
* {@link JsonMixinModule}.
30+
*
31+
* @author Guirong Hu
32+
* @see JsonMixinModule
33+
* @since 2.7.0
34+
*/
35+
@Target(ElementType.TYPE)
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@Documented
38+
public @interface JsonMixin {
39+
40+
/**
41+
* Alias for the {@link #type()} attribute. Allows for more concise annotation
42+
* declarations e.g.: {@code @JsonMixin(MyType.class)} instead of
43+
* {@code @JsonMixin(type=MyType.class)}.
44+
* @return the mixed-in classes
45+
* @since 2.7.0
46+
*/
47+
@AliasFor("type")
48+
Class<?>[] value() default {};
49+
50+
/**
51+
* The types that are handled by the provided mix-in class. {@link #value()} is an
52+
* alias for (and mutually exclusive with) this attribute.
53+
* @return the mixed-in classes
54+
* @since 2.7.0
55+
*/
56+
@AliasFor("value")
57+
Class<?>[] type() default {};
58+
59+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jackson;
18+
19+
import java.io.IOException;
20+
import java.util.Collection;
21+
22+
import com.fasterxml.jackson.databind.Module;
23+
import com.fasterxml.jackson.databind.module.SimpleModule;
24+
25+
import org.springframework.beans.factory.InitializingBean;
26+
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
27+
import org.springframework.beans.factory.config.BeanDefinition;
28+
import org.springframework.context.ApplicationContext;
29+
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
30+
import org.springframework.core.annotation.MergedAnnotation;
31+
import org.springframework.core.annotation.MergedAnnotations;
32+
import org.springframework.core.type.classreading.MetadataReader;
33+
import org.springframework.util.Assert;
34+
import org.springframework.util.ClassUtils;
35+
import org.springframework.util.ObjectUtils;
36+
import org.springframework.util.StringUtils;
37+
38+
/**
39+
* Spring Bean and Jackson {@link Module} to register {@link JsonMixin @JsonMixin}
40+
* annotated beans.
41+
*
42+
* @author Guirong Hu
43+
* @since 2.7.0
44+
* @see JsonMixin
45+
*/
46+
public class JsonMixinModule extends SimpleModule implements InitializingBean {
47+
48+
private final ApplicationContext context;
49+
50+
private final Collection<String> basePackages;
51+
52+
/**
53+
* Create a new {@link JsonMixinModule} instance.
54+
* @param context the source application context
55+
* @param basePackages the packages to check for annotated classes
56+
*/
57+
public JsonMixinModule(ApplicationContext context, Collection<String> basePackages) {
58+
Assert.notNull(context, "Context must not be null");
59+
this.context = context;
60+
this.basePackages = basePackages;
61+
}
62+
63+
@Override
64+
public void afterPropertiesSet() throws Exception {
65+
if (ObjectUtils.isEmpty(this.basePackages)) {
66+
return;
67+
}
68+
JsonMixinComponentScanner scanner = new JsonMixinComponentScanner();
69+
scanner.setEnvironment(this.context.getEnvironment());
70+
scanner.setResourceLoader(this.context);
71+
72+
for (String basePackage : this.basePackages) {
73+
if (StringUtils.hasText(basePackage)) {
74+
for (BeanDefinition candidate : scanner.findCandidateComponents(basePackage)) {
75+
addJsonMixin(ClassUtils.forName(candidate.getBeanClassName(), this.context.getClassLoader()));
76+
}
77+
}
78+
}
79+
}
80+
81+
private void addJsonMixin(Class<?> mixinClass) {
82+
MergedAnnotation<JsonMixin> annotation = MergedAnnotations
83+
.from(mixinClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).get(JsonMixin.class);
84+
Class<?>[] targetTypes = annotation.getClassArray("type");
85+
if (ObjectUtils.isEmpty(targetTypes)) {
86+
return;
87+
}
88+
for (Class<?> targetType : targetTypes) {
89+
setMixInAnnotation(targetType, mixinClass);
90+
}
91+
}
92+
93+
static class JsonMixinComponentScanner extends ClassPathScanningCandidateComponentProvider {
94+
95+
@Override
96+
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
97+
return true;
98+
}
99+
100+
@Override
101+
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
102+
return beanDefinition.getMetadata().hasAnnotation(JsonMixin.class.getName());
103+
}
104+
105+
}
106+
107+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jackson;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.context.annotation.Import;
26+
import org.springframework.core.annotation.AliasFor;
27+
28+
/**
29+
* Configures the base packages used by auto-configuration when scanning for mix-in
30+
* classes. One of {@link #basePackageClasses()}, {@link #basePackages()} or its alias
31+
* {@link #value()} may be specified to define specific packages to scan. If specific
32+
* packages are not defined scanning will occur from the package of the class with this
33+
* annotation.
34+
*
35+
* @author Guirong Hu
36+
* @since 2.7.0
37+
* @see JsonMixinScanPackages
38+
*/
39+
@Target(ElementType.TYPE)
40+
@Retention(RetentionPolicy.RUNTIME)
41+
@Documented
42+
@Import(JsonMixinScanPackages.Registrar.class)
43+
public @interface JsonMixinScan {
44+
45+
/**
46+
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation
47+
* declarations e.g.: {@code @JsonMixinScan("org.my.pkg")} instead of
48+
* {@code @JsonMixinScan(basePackages="org.my.pkg")}.
49+
* @return the base packages to scan
50+
*/
51+
@AliasFor("basePackages")
52+
String[] value() default {};
53+
54+
/**
55+
* Base packages to scan for mix-in classes. {@link #value()} is an alias for (and
56+
* mutually exclusive with) this attribute.
57+
* <p>
58+
* Use {@link #basePackageClasses()} for a type-safe alternative to String-based
59+
* package names.
60+
* @return the base packages to scan
61+
*/
62+
@AliasFor("value")
63+
String[] basePackages() default {};
64+
65+
/**
66+
* Type-safe alternative to {@link #basePackages()} for specifying the packages to
67+
* scan for mix-in classes. The package of each class specified will be scanned.
68+
* <p>
69+
* Consider creating a special no-op marker class or interface in each package that
70+
* serves no purpose other than being referenced by this attribute.
71+
* @return classes from the base packages to scan
72+
*/
73+
Class<?>[] basePackageClasses() default {};
74+
75+
}

0 commit comments

Comments
 (0)