Detect accidental misconfiguration of JsonMixin annotation

See gh-42592

Closes gh-42592
This commit is contained in:
Dmytro Nosan 2024-10-10 20:07:47 +03:00 committed by Andy Wilkinson
parent ec6ab4f9b9
commit c9d5351fcf
3 changed files with 47 additions and 3 deletions

View File

@ -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.context.annotation.ClassPathScanningCandidateComponen
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 @@ public final class JsonMixinModuleEntries {
MergedAnnotation<JsonMixin> 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);
}

View File

@ -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 com.fasterxml.jackson.databind.ObjectMapper;
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 @@ class JsonMixinModuleTests {
}
}
@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);

View File

@ -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();
}