Skip to content

Commit b7ab74a

Browse files
committed
Merge pull request #42592 from nosan
* gh-42592: Detect accidental misconfiguration of JsonMixin annotation Closes gh-42592
2 parents ec6ab4f + c9d5351 commit b7ab74a

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -29,6 +29,7 @@
2929
import org.springframework.core.annotation.MergedAnnotation;
3030
import org.springframework.core.annotation.MergedAnnotations;
3131
import org.springframework.core.type.filter.AnnotationTypeFilter;
32+
import org.springframework.util.Assert;
3233
import org.springframework.util.ClassUtils;
3334
import org.springframework.util.ObjectUtils;
3435
import org.springframework.util.StringUtils;
@@ -89,7 +90,10 @@ private static void registerMixinClass(Builder builder, Class<?> mixinClass) {
8990
MergedAnnotation<JsonMixin> annotation = MergedAnnotations
9091
.from(mixinClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
9192
.get(JsonMixin.class);
92-
for (Class<?> targetType : annotation.getClassArray("type")) {
93+
Class<?>[] types = annotation.getClassArray("type");
94+
Assert.notEmpty(types,
95+
"@JsonMixin annotation on class '" + mixinClass.getName() + "' does not specify any types");
96+
for (Class<?> targetType : types) {
9397
builder.and(targetType, mixinClass);
9498
}
9599

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -24,16 +24,19 @@
2424
import org.junit.jupiter.api.AfterEach;
2525
import org.junit.jupiter.api.Test;
2626

27+
import org.springframework.beans.factory.BeanCreationException;
2728
import org.springframework.boot.jackson.scan.a.RenameMixInClass;
2829
import org.springframework.boot.jackson.scan.b.RenameMixInAbstractClass;
2930
import org.springframework.boot.jackson.scan.c.RenameMixInInterface;
3031
import org.springframework.boot.jackson.scan.d.EmptyMixInClass;
32+
import org.springframework.boot.jackson.scan.f.EmptyMixIn;
3133
import org.springframework.boot.jackson.types.Name;
3234
import org.springframework.boot.jackson.types.NameAndAge;
3335
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3436
import org.springframework.util.ClassUtils;
3537

3638
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3740

3841
/**
3942
* Tests for {@link JsonMixinModule}.
@@ -51,6 +54,14 @@ void closeContext() {
5154
}
5255
}
5356

57+
@Test
58+
void jsonWithModuleEmptyMixInWithEmptyTypesShouldFailed() {
59+
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> load(EmptyMixIn.class))
60+
.withMessageContaining("Error creating bean with name 'jsonMixinModule'")
61+
.withStackTraceContaining("@JsonMixin annotation on class "
62+
+ "'org.springframework.boot.jackson.scan.f.EmptyMixIn' does not specify any types");
63+
}
64+
5465
@Test
5566
void jsonWithModuleWithRenameMixInClassShouldBeMixedIn() throws Exception {
5667
load(RenameMixInClass.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2012-2024 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.f;
18+
19+
import com.fasterxml.jackson.annotation.JsonProperty;
20+
21+
import org.springframework.boot.jackson.JsonMixin;
22+
23+
@JsonMixin
24+
public interface EmptyMixIn {
25+
26+
@JsonProperty("username")
27+
String getName();
28+
29+
}

0 commit comments

Comments
 (0)