Skip to content

Commit 64be4d3

Browse files
committed
Merge pull request #30152 from terminux
* gh-30152: Polish Simplify registration of Jackson mixin types Closes gh-30152
2 parents 45f393b + 685d2d4 commit 64be4d3

File tree

10 files changed

+422
-1
lines changed

10 files changed

+422
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@
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;
4951
import org.springframework.context.ApplicationContext;
5052
import org.springframework.context.annotation.Bean;
5153
import org.springframework.context.annotation.Configuration;
@@ -93,6 +95,13 @@ public JsonComponentModule jsonComponentModule() {
9395
return new JsonComponentModule();
9496
}
9597

98+
@Bean
99+
public JsonMixinModule jsonMixinModule(ApplicationContext context) {
100+
List<String> packages = AutoConfigurationPackages.has(context) ? AutoConfigurationPackages.get(context)
101+
: Collections.emptyList();
102+
return new JsonMixinModule(context, packages);
103+
}
104+
96105
@Configuration(proxyBeanMethods = false)
97106
@ConditionalOnClass(Jackson2ObjectMapperBuilder.class)
98107
static class JacksonObjectMapperConfiguration {

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

Lines changed: 19 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.
@@ -47,11 +47,14 @@
4747
import com.fasterxml.jackson.databind.module.SimpleModule;
4848
import com.fasterxml.jackson.databind.util.StdDateFormat;
4949
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
50+
import org.assertj.core.api.InstanceOfAssertFactories;
5051
import org.junit.jupiter.api.Test;
5152

53+
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
5254
import org.springframework.boot.autoconfigure.AutoConfigurations;
5355
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
5456
import org.springframework.boot.jackson.JsonComponent;
57+
import org.springframework.boot.jackson.JsonMixinModule;
5558
import org.springframework.boot.jackson.JsonObjectSerializer;
5659
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
5760
import org.springframework.context.annotation.Bean;
@@ -90,6 +93,16 @@ void doubleModuleRegistration() {
9093
});
9194
}
9295

96+
@Test
97+
void jsonMixinModuleShouldBeAutoConfiguredWithBasePackages() {
98+
this.contextRunner.withUserConfiguration(MixinConfiguration.class).run((context) -> {
99+
assertThat(context).hasSingleBean(JsonMixinModule.class);
100+
JsonMixinModule module = context.getBean(JsonMixinModule.class);
101+
assertThat(module).extracting("basePackages", InstanceOfAssertFactories.list(String.class))
102+
.containsExactly(MixinConfiguration.class.getPackage().getName());
103+
});
104+
}
105+
93106
@Test
94107
void noCustomDateFormat() {
95108
this.contextRunner.run((context) -> {
@@ -631,4 +644,9 @@ void setBirthDate(Date birthDate) {
631644

632645
}
633646

647+
@AutoConfigurationPackage
648+
static class MixinConfiguration {
649+
650+
}
651+
634652
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/json.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ include::code:object/MyJsonComponent[]
4040

4141

4242

43+
[[features.json.jackson.mixins]]
44+
==== Mixins
45+
Jackson has support for mixins that can be used to mix additional annotations into those already declared on a target class.
46+
Spring Boot's Jackson auto-configuration will scan your application's packages for classes annotated with `@JsonMixin` and register them with the auto-configured `ObjectMapper`.
47+
The registration is performed by Spring Boot's `JsonMixinModule`.
48+
49+
50+
4351
[[features.json.gson]]
4452
=== Gson
4553
Auto-configuration for Gson is provided.
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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.util.Collection;
20+
21+
import com.fasterxml.jackson.databind.Module;
22+
import com.fasterxml.jackson.databind.module.SimpleModule;
23+
24+
import org.springframework.beans.factory.InitializingBean;
25+
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
26+
import org.springframework.beans.factory.config.BeanDefinition;
27+
import org.springframework.context.ApplicationContext;
28+
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
29+
import org.springframework.core.annotation.MergedAnnotation;
30+
import org.springframework.core.annotation.MergedAnnotations;
31+
import org.springframework.core.type.filter.AnnotationTypeFilter;
32+
import org.springframework.util.Assert;
33+
import org.springframework.util.ClassUtils;
34+
import org.springframework.util.ObjectUtils;
35+
import org.springframework.util.StringUtils;
36+
37+
/**
38+
* Spring Bean and Jackson {@link Module} to find and
39+
* {@link SimpleModule#setMixInAnnotation(Class, Class) register}
40+
* {@link JsonMixin @JsonMixin}-annotated classes.
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+
for (String basePackage : this.basePackages) {
72+
if (StringUtils.hasText(basePackage)) {
73+
for (BeanDefinition candidate : scanner.findCandidateComponents(basePackage)) {
74+
addJsonMixin(ClassUtils.forName(candidate.getBeanClassName(), this.context.getClassLoader()));
75+
}
76+
}
77+
}
78+
}
79+
80+
private void addJsonMixin(Class<?> mixinClass) {
81+
MergedAnnotation<JsonMixin> annotation = MergedAnnotations
82+
.from(mixinClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).get(JsonMixin.class);
83+
for (Class<?> targetType : annotation.getClassArray("type")) {
84+
setMixInAnnotation(targetType, mixinClass);
85+
}
86+
}
87+
88+
static class JsonMixinComponentScanner extends ClassPathScanningCandidateComponentProvider {
89+
90+
JsonMixinComponentScanner() {
91+
addIncludeFilter(new AnnotationTypeFilter(JsonMixin.class));
92+
}
93+
94+
@Override
95+
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
96+
return true;
97+
}
98+
99+
}
100+
101+
}
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.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
24+
import com.fasterxml.jackson.databind.Module;
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.Test;
28+
29+
import org.springframework.boot.jackson.scan.a.RenameMixInClass;
30+
import org.springframework.boot.jackson.scan.b.RenameMixInAbstractClass;
31+
import org.springframework.boot.jackson.scan.c.RenameMixInInterface;
32+
import org.springframework.boot.jackson.scan.d.EmptyMixInClass;
33+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
34+
import org.springframework.util.ClassUtils;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
38+
39+
/**
40+
* Tests for {@link JsonMixinModule}.
41+
*
42+
* @author Guirong Hu
43+
*/
44+
class JsonMixinModuleTests {
45+
46+
private AnnotationConfigApplicationContext context;
47+
48+
@AfterEach
49+
void closeContext() {
50+
if (this.context != null) {
51+
this.context.close();
52+
}
53+
}
54+
55+
@Test
56+
void createWhenContextIsNullShouldThrowException() {
57+
assertThatIllegalArgumentException().isThrownBy(() -> new JsonMixinModule(null, Collections.emptyList()))
58+
.withMessageContaining("Context must not be null");
59+
}
60+
61+
@Test
62+
void jsonWithModuleWithRenameMixInClassShouldBeMixedIn() throws Exception {
63+
load(RenameMixInClass.class);
64+
JsonMixinModule module = this.context.getBean(JsonMixinModule.class);
65+
assertMixIn(module, new Name("spring"), "{\"username\":\"spring\"}");
66+
assertMixIn(module, new NameAndAge("spring", 100), "{\"age\":100,\"username\":\"spring\"}");
67+
}
68+
69+
@Test
70+
void jsonWithModuleWithEmptyMixInClassShouldNotBeMixedIn() throws Exception {
71+
load(EmptyMixInClass.class);
72+
JsonMixinModule module = this.context.getBean(JsonMixinModule.class);
73+
assertMixIn(module, new Name("spring"), "{\"name\":\"spring\"}");
74+
assertMixIn(module, new NameAndAge("spring", 100), "{\"name\":\"spring\",\"age\":100}");
75+
}
76+
77+
@Test
78+
void jsonWithModuleWithRenameMixInAbstractClassShouldBeMixedIn() throws Exception {
79+
load(RenameMixInAbstractClass.class);
80+
JsonMixinModule module = this.context.getBean(JsonMixinModule.class);
81+
assertMixIn(module, new NameAndAge("spring", 100), "{\"age\":100,\"username\":\"spring\"}");
82+
}
83+
84+
@Test
85+
void jsonWithModuleWithRenameMixInInterfaceShouldBeMixedIn() throws Exception {
86+
load(RenameMixInInterface.class);
87+
JsonMixinModule module = this.context.getBean(JsonMixinModule.class);
88+
assertMixIn(module, new NameAndAge("spring", 100), "{\"age\":100,\"username\":\"spring\"}");
89+
}
90+
91+
private void load(Class<?>... basePackageClasses) {
92+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
93+
List<String> basePackages = Arrays.stream(basePackageClasses).map(ClassUtils::getPackageName)
94+
.collect(Collectors.toList());
95+
context.registerBean(JsonMixinModule.class, () -> new JsonMixinModule(context, basePackages));
96+
context.refresh();
97+
this.context = context;
98+
}
99+
100+
private void assertMixIn(Module module, Name value, String expectedJson) throws Exception {
101+
ObjectMapper mapper = new ObjectMapper();
102+
mapper.registerModule(module);
103+
String json = mapper.writeValueAsString(value);
104+
assertThat(json).isEqualToIgnoringWhitespace(expectedJson);
105+
}
106+
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.scan.a;
18+
19+
import com.fasterxml.jackson.annotation.JsonProperty;
20+
21+
import org.springframework.boot.jackson.JsonMixin;
22+
import org.springframework.boot.jackson.Name;
23+
import org.springframework.boot.jackson.NameAndAge;
24+
25+
@JsonMixin(type = { Name.class, NameAndAge.class })
26+
public class RenameMixInClass {
27+
28+
@JsonProperty("username")
29+
String getName() {
30+
return null;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)