generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix type argument annotation inheritance
- Loading branch information
1 parent
bad548c
commit 5ca9504
Showing
3 changed files
with
177 additions
and
4 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
111 changes: 111 additions & 0 deletions
111
...c/test/groovy/io/micronaut/validation/visitor/ValidatedTypeArgumentInheritanceSpec.groovy
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,111 @@ | ||
package io.micronaut.validation.visitor | ||
|
||
import io.micronaut.annotation.processing.test.AbstractTypeElementSpec | ||
import jakarta.validation.constraints.NotBlank | ||
import jakarta.validation.constraints.NotNull | ||
import jakarta.validation.constraints.Size | ||
|
||
class ValidatedTypeArgumentInheritanceSpec extends AbstractTypeElementSpec { | ||
final static String VALIDATED_ANN = "io.micronaut.validation.Validated"; | ||
|
||
void "test constraints on inherited generic parameters make method @Validated"() { | ||
given: | ||
def definition = buildBeanDefinition('test.Test',''' | ||
package test; | ||
import java.util.List; | ||
import jakarta.validation.constraints.NotBlank; | ||
@jakarta.inject.Singleton | ||
class Test implements TestBase { | ||
@Override | ||
public void setList(List<String> list) { | ||
} | ||
} | ||
interface TestBase { | ||
@io.micronaut.context.annotation.Executable | ||
void setList(List<@NotBlank String> list); | ||
} | ||
''') | ||
when: | ||
def method = definition.getRequiredMethod("setList", List<String>) | ||
|
||
then: | ||
method.hasStereotype(VALIDATED_ANN) | ||
method.arguments.size() == 1 | ||
method.arguments[0].annotationMetadata.hasAnnotation("io.micronaut.validation.annotation.ValidatedElement") | ||
method.arguments[0].typeParameters.size() == 1 | ||
method.arguments[0].typeParameters[0].annotationMetadata.hasAnnotation(NotBlank) | ||
} | ||
|
||
void "test constraints on inherited generic parameters make method @Validated deep"() { | ||
given: | ||
def definition = buildBeanDefinition('test.Test',''' | ||
package test; | ||
import java.util.*; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
@jakarta.inject.Singleton | ||
class Test implements TestBase { | ||
@Override | ||
public void map(Map<@NotBlank String, List<@NotNull String>> list) { | ||
} | ||
} | ||
interface TestBase { | ||
@io.micronaut.context.annotation.Executable | ||
void map(Map<String, List<@NotBlank String>> list); | ||
} | ||
''') | ||
when: | ||
def method = definition.getRequiredMethod("map", Map<String, List<String>>) | ||
|
||
then: | ||
method.hasStereotype(VALIDATED_ANN) | ||
method.arguments.size() == 1 | ||
method.arguments[0].annotationMetadata.hasAnnotation("io.micronaut.validation.annotation.ValidatedElement") | ||
method.arguments[0].typeParameters.size() == 2 | ||
method.arguments[0].typeParameters[0].annotationMetadata.hasAnnotation(NotBlank) | ||
method.arguments[0].typeParameters[1].typeParameters.length == 1 | ||
method.arguments[0].typeParameters[1].typeParameters[0].annotationMetadata.hasAnnotation(NotBlank) | ||
method.arguments[0].typeParameters[1].typeParameters[0].annotationMetadata.hasAnnotation(NotNull) | ||
} | ||
|
||
void "test constraints from inherited class generic parameters make method @Validated"() { | ||
given: | ||
def definition = buildBeanDefinition('test.Test',''' | ||
package test; | ||
import java.util.*; | ||
import jakarta.validation.constraints.Size; | ||
@jakarta.inject.Singleton | ||
class Test extends AbstractTest { | ||
@Override | ||
public void map(Map<String, @Size(min=2) String> value) { | ||
} | ||
} | ||
abstract class AbstractTest { | ||
void map(Map<String, @Size(min=2) String> value) { | ||
} | ||
} | ||
''') | ||
when: | ||
def method = definition.getRequiredMethod("map", Map<String, List<String>>) | ||
|
||
then: | ||
method.hasStereotype(VALIDATED_ANN) | ||
method.arguments.size() == 1 | ||
method.arguments[0].annotationMetadata.hasAnnotation("io.micronaut.validation.annotation.ValidatedElement") | ||
method.arguments[0].typeParameters.size() == 2 | ||
var anns = method.arguments[0].typeParameters[1].annotationMetadata.getAnnotationValuesByType(Size) | ||
anns.size() == 1 | ||
anns.get(0).intValue("min").get() == 2 | ||
} | ||
|
||
} |