-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support @JacksonAnnotationsInside on meta/combo annotations (#495)
- Loading branch information
1 parent
8598b9f
commit ddd7c98
Showing
15 changed files
with
388 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...ma-generator/src/main/java/com/github/victools/jsonschema/generator/AnnotationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2024 VicTools. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.github.victools.jsonschema.generator; | ||
|
||
import com.fasterxml.classmate.members.ResolvedMember; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.AnnotatedElement; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* Helper class providing with standard mechanism to resolve annotations on annotated entities. | ||
* | ||
* @since 4.37.0 | ||
*/ | ||
public final class AnnotationHelper { | ||
|
||
private AnnotationHelper() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Resolves the specified annotation on the given resolved member and resolve nested annotations. | ||
* | ||
* @param <A> the generic type of the annotation | ||
* @param member where to look for the specified annotation | ||
* @param annotationClass the class of the annotation to look for | ||
* @param metaAnnotationCheck the predicate indicating nested annotations | ||
* @return an empty entry if not found | ||
*/ | ||
public static <A extends Annotation> Optional<A> resolveAnnotation(ResolvedMember<?> member, Class<A> annotationClass, | ||
Predicate<Annotation> metaAnnotationCheck) { | ||
final A annotation = member.getAnnotations().get(annotationClass); | ||
if (annotation == null) { | ||
return AnnotationHelper.resolveNestedAnnotations(StreamSupport.stream(member.getAnnotations().spliterator(), false), | ||
annotationClass, metaAnnotationCheck); | ||
} | ||
return Optional.of(annotation); | ||
} | ||
|
||
/** | ||
* Select the instance of the specified annotation type from the given list. | ||
* | ||
* <p>Also considering meta annotations (i.e., annotations on annotations) if a meta annotation is | ||
* deemed eligible according to the given {@code Predicate}.</p> | ||
* | ||
* @param <A> the generic type of the annotation | ||
* @param annotationList a list of annotations to look into | ||
* @param annotationClass the class of the annotation to look for | ||
* @param metaAnnotationCheck the predicate indicating nested annotations | ||
* @return an empty entry if not found | ||
*/ | ||
public static <A extends Annotation> Optional<A> resolveAnnotation(List<Annotation> annotationList, Class<A> annotationClass, | ||
Predicate<Annotation> metaAnnotationCheck) { | ||
final Optional<Annotation> annotation = annotationList.stream() | ||
.filter(annotationClass::isInstance) | ||
.findFirst(); | ||
if (annotation.isPresent()) { | ||
return annotation.map(annotationClass::cast); | ||
} | ||
return AnnotationHelper.resolveNestedAnnotations(annotationList.stream(), annotationClass, metaAnnotationCheck); | ||
} | ||
|
||
/** | ||
* Select the instance of the specified annotation type from the given annotatedElement's annotations. | ||
* | ||
* <p>Also considering meta annotations (i.e., annotations on annotations) if a meta annotation is | ||
* deemed eligible according to the given <code>metaAnnotationPredicate</code>.</p> | ||
* | ||
* @param <A> the generic type of the annotation | ||
* @param annotatedElement where to look for the specified annotation | ||
* @param annotationClass the class of the annotation to look for | ||
* @param metaAnnotationCheck the predicate indicating meta annotations | ||
* @return an empty entry if not found | ||
*/ | ||
public static <A extends Annotation> Optional<A> resolveAnnotation(AnnotatedElement annotatedElement, Class<A> annotationClass, | ||
Predicate<Annotation> metaAnnotationCheck) { | ||
final A annotation = annotatedElement.getAnnotation(annotationClass); | ||
if (annotation == null) { | ||
return AnnotationHelper.resolveNestedAnnotations(Arrays.stream(annotatedElement.getAnnotations()), | ||
annotationClass, metaAnnotationCheck); | ||
} | ||
return Optional.of(annotation); | ||
} | ||
|
||
private static <A extends Annotation> Optional<A> resolveNestedAnnotations(Stream<Annotation> initialAnnotations, Class<A> annotationClass, | ||
Predicate<Annotation> metaAnnotationCheck) { | ||
List<Annotation> annotations = AnnotationHelper.extractAnnotationsFromMetaAnnotations(initialAnnotations, metaAnnotationCheck); | ||
while (!annotations.isEmpty()) { | ||
final Optional<Annotation> directAnnotation = annotations.stream() | ||
.filter(annotationClass::isInstance) | ||
.findFirst(); | ||
if (directAnnotation.isPresent()) { | ||
return directAnnotation.map(annotationClass::cast); | ||
} | ||
annotations = AnnotationHelper.extractAnnotationsFromMetaAnnotations(annotations.stream(), metaAnnotationCheck); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static List<Annotation> extractAnnotationsFromMetaAnnotations(Stream<Annotation> annotations, Predicate<Annotation> metaAnnotationCheck) { | ||
return annotations.filter(metaAnnotationCheck) | ||
.flatMap(a -> Arrays.stream(a.annotationType().getAnnotations())) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
...enerator/src/test/java/com/github/victools/jsonschema/generator/AnnotationHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2024 VicTools. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.github.victools.jsonschema.generator; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Unit test class dedicated to the validation of {@link AnnotationHelper}. | ||
*/ | ||
public class AnnotationHelperTest { | ||
|
||
static Stream<Arguments> annotationLookupScenarios() { | ||
return Stream.of( | ||
Arguments.of(NonAnnotatedClass.class, Optional.empty()), | ||
Arguments.of(AnnotatedClassWithUselessAnnotations.class, Optional.empty()), | ||
Arguments.of(DirectlyAnnotatedClass.class, Optional.of("")), | ||
Arguments.of(BothDirectAndIndirectlyAnnotatedClass.class, Optional.of("direct value")), | ||
Arguments.of(IndirectlyAnnotatedClass.class, Optional.of("first combo annotation value")), | ||
Arguments.of(BreadthFirstAnnotatedClass.class, Optional.of("first combo annotation value")) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("annotationLookupScenarios") | ||
void resolveAnnotation_AnnotatedElement_respects_annotationLookupScenarios(Class<?> annotatedClass, Optional<String> expectedAnnotationValue) { | ||
Optional<String> value = AnnotationHelper.resolveAnnotation(annotatedClass, TargetAnnotation.class, metaAnnotationPredicate()).map(TargetAnnotation::value); | ||
Assertions.assertEquals(expectedAnnotationValue, value); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("annotationLookupScenarios") | ||
void resolveAnnotation_List_respects_annotationLookupScenarios(Class<?> annotatedClass, Optional<String> expectedAnnotationValue) { | ||
Optional<String> value = AnnotationHelper.resolveAnnotation(Arrays.asList(annotatedClass.getAnnotations()), TargetAnnotation.class, metaAnnotationPredicate()).map(TargetAnnotation::value); | ||
Assertions.assertEquals(expectedAnnotationValue, value); | ||
} | ||
|
||
private static Predicate<Annotation> metaAnnotationPredicate() { | ||
return (annotation) -> annotation.annotationType().isAnnotationPresent(MetaAnnotation.class); | ||
} | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface TargetAnnotation { | ||
String value() default ""; | ||
} | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface MetaAnnotation {} | ||
|
||
@TargetAnnotation | ||
private static class DirectlyAnnotatedClass { | ||
} | ||
|
||
private static class NonAnnotatedClass { | ||
} | ||
|
||
@UselessFirstComboAnnotation | ||
@UselessSecondComboAnnotation | ||
private static class AnnotatedClassWithUselessAnnotations { | ||
|
||
} | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@MetaAnnotation | ||
private @interface UselessFirstComboAnnotation { | ||
} | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@MetaAnnotation | ||
private @interface UselessSecondComboAnnotation { | ||
} | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@MetaAnnotation | ||
@TargetAnnotation("first combo annotation value") | ||
private @interface FirstComboAnnotation { | ||
} | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@MetaAnnotation | ||
@TargetAnnotation("second combo annotation value") | ||
private @interface SecondComboAnnotation { | ||
} | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@MetaAnnotation | ||
@SecondComboAnnotation | ||
private @interface ThirdComboAnnotation { | ||
} | ||
|
||
@FirstComboAnnotation | ||
@SecondComboAnnotation | ||
private static class IndirectlyAnnotatedClass { | ||
} | ||
|
||
@TargetAnnotation("direct value") | ||
@FirstComboAnnotation | ||
@SecondComboAnnotation | ||
private static class BothDirectAndIndirectlyAnnotatedClass { | ||
} | ||
|
||
@ThirdComboAnnotation | ||
@FirstComboAnnotation | ||
private static class BreadthFirstAnnotatedClass {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.