Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: props with optional getters should be validated #165

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.micronaut.context.annotation.Prototype
import io.micronaut.context.annotation.Value
import io.micronaut.context.exceptions.BeanInstantiationException
import io.micronaut.core.annotation.Introspected
import io.micronaut.core.annotation.Nullable
import io.micronaut.core.reflect.ClassUtils
import io.micronaut.validation.Validated
import io.micronaut.validation.validator.resolver.CompositeTraversableResolver
Expand All @@ -17,9 +18,12 @@ import jakarta.validation.constraints.Max
import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Pattern
import jakarta.validation.constraints.Size
import jakarta.validation.metadata.BeanDescriptor
import spock.lang.AutoCleanup
import spock.lang.Issue
import spock.lang.PendingFeature
import spock.lang.Shared
import spock.lang.Specification

Expand Down Expand Up @@ -153,6 +157,21 @@ class ValidatorSpec extends Specification {

}

@PendingFeature
@Issue("https://github.com/micronaut-projects/micronaut-validation/pull/165")
void "test validate bean property with Optional getter"() {
given:
OptionalGetters g = new OptionalGetters(alpha: "non alpha with space");
def violations = validator.validateProperty(g, "alpha")

expect:
violations.size() == 1
violations[0].invalidValue == 'non alpha with space'
violations[0].propertyPath.iterator().next().name == 'alpha'
violations[0].constraintDescriptor.annotation instanceof Pattern
violations[0].message == 'must match "[a-z]+"'
}

void "test validate value"() {
given:
def violations = validator.validateValue(Book, "title", "").sort { it.propertyPath.iterator().next().name }
Expand Down Expand Up @@ -1047,6 +1066,22 @@ class Author {
Book favouriteBook
}

@Introspected
class OptionalGetters {

@Nullable
@Pattern(regexp = "[a-z]+")
String alpha

Optional<String> getAlpha() {
Optional.ofNullable(alpha)
}

void setAlpha(String alpha) {
this.alpha = alpha
}
}

@Introspected
@Prototype
class ArrayTest {
Expand Down
Loading