diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonMixinModuleEntries.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonMixinModuleEntries.java index 828cd44599f7..4cb0e8d9b0e5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonMixinModuleEntries.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jackson/JsonMixinModuleEntries.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.type.filter.AnnotationTypeFilter; +import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -89,7 +90,10 @@ private static void registerMixinClass(Builder builder, Class mixinClass) { MergedAnnotation annotation = MergedAnnotations .from(mixinClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY) .get(JsonMixin.class); - for (Class targetType : annotation.getClassArray("type")) { + Class[] types = annotation.getClassArray("type"); + Assert.notEmpty(types, + "@JsonMixin annotation on class '" + mixinClass.getName() + "' does not specify any types"); + for (Class targetType : types) { builder.and(targetType, mixinClass); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/JsonMixinModuleTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/JsonMixinModuleTests.java index 768a1c0b8da4..6bedd39637c8 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/JsonMixinModuleTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/JsonMixinModuleTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,16 +24,19 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.jackson.scan.a.RenameMixInClass; import org.springframework.boot.jackson.scan.b.RenameMixInAbstractClass; import org.springframework.boot.jackson.scan.c.RenameMixInInterface; import org.springframework.boot.jackson.scan.d.EmptyMixInClass; +import org.springframework.boot.jackson.scan.f.EmptyMixIn; import org.springframework.boot.jackson.types.Name; import org.springframework.boot.jackson.types.NameAndAge; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.util.ClassUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link JsonMixinModule}. @@ -51,6 +54,14 @@ void closeContext() { } } + @Test + void jsonWithModuleEmptyMixInWithEmptyTypesShouldFailed() { + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> load(EmptyMixIn.class)) + .withMessageContaining("Error creating bean with name 'jsonMixinModule'") + .withStackTraceContaining("@JsonMixin annotation on class " + + "'org.springframework.boot.jackson.scan.f.EmptyMixIn' does not specify any types"); + } + @Test void jsonWithModuleWithRenameMixInClassShouldBeMixedIn() throws Exception { load(RenameMixInClass.class); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/scan/f/EmptyMixIn.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/scan/f/EmptyMixIn.java new file mode 100644 index 000000000000..d53635717380 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jackson/scan/f/EmptyMixIn.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.jackson.scan.f; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import org.springframework.boot.jackson.JsonMixin; + +@JsonMixin +public interface EmptyMixIn { + + @JsonProperty("username") + String getName(); + +}