-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
Bean validation doesn't work on Kotlin coroutines controller parameters #23499
Comments
|
I am not entirely sure what we should do here. The issue comes from Given those uncertainties, it seems more reasonable to postpone this issue for 5.3. |
Hello, @sdeleuze , any updates? Today, I think about another annotation something like KValidated as temporary solution, For example you can add new annotation KValidated which should be used only in kotlin. And after add KMethodValidationInterceptor with similary behaviour as MethodValidationInterceptor, what do you think about this? And can you describe the problem with solution please?. Why we can't just use CoroutinesUtils.invokeSuspendingFunction(method, getBean(), args); as it is used in InvocableHandlerMethod? |
@kostya05983 Even after adding support for Coroutines via
#22462 fix should be all what is needed on Spring side. |
For reference, this is the hibernate issue: https://hibernate.atlassian.net/browse/HV-1638 |
Removing the usage of KotlinReflectionParameterNameDiscoverer in LocalValidatorFactoryBean and just delegate to Hibernate Validator fixed the use case with suspending functions. Closes spring-projectsgh-23499
Based on a quick test, it seems just removing the usage of Could people interested in getting that fix test my changes on sdeleuze@gh-23499 and provide a feedback here? |
i don't think hibernate validation will compatible with the kotlin suspend function. https://hibernate.atlassian.net/browse/HV-1638 |
@sdeleuze |
On Spring side, I have the feeling that Some Kotlin use cases may still be broken until Kotlin team move forward on https://youtrack.jetbrains.com/issue/KT-40857, but I could ask them to move forward on that issue. Could you please test my branch and let me know how it goes for typical use case? |
i will have a test |
@cjdxhjj Any chance you could test and provide a feedback? |
@sdeleuze i'm sorry for the slow response, i'm on holidy, i would try it as soon as possible. |
@sdeleuze i have just do a simple test with |
Bean validation on suspending function parameters should be fixed as of Spring Framework 6.0.5, I don't think the fix is doable easily on 5.3.x so I won't backport it. Validation of suspending function return values remains unsupported as Hibernate Validator is not Coroutines aware, but I think parameter validation was the most critical need. |
@sdeleuze thanks a lot |
Hello! I'm not sure this is a related issue, though, let's see if we should open a new ticket. After upgrading from Spring Boot 3.0.2 to 3.0.4 validations on GraphQL controller parameters work as expected, but I'm now having a problem with custom ConstraintValidators which are not triggered at all. Please see a sample code: @Controller
@Validated
class CompanyController {
@PreAuthorize("hasRole('ROLE_COMPANY_EDIT')")
@MutationMapping("createCompany")
suspend fun createCompany(
@Argument("input")
@Valid
companyInput: CompanyInput
): CompanyResponse {
// TODO..
}
}
@CompanyNameUniquePerWhitelabelId
data class CompanyInput(
val companyType: CompanyType,
val name: String,
@IsIso2Country val registeredAddressCountryCode: Int
)
@MustBeDocumented
@Constraint(validatedBy = [CompanyNameUniquePerWhitelabelIdValidator::class])
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class CompanyNameUniquePerWhitelabelId(
val message: String = "This company name already exists for Provided whitelabel.",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Any>> = []
)
class CompanyNameUniquePerWhitelabelIdValidator : ConstraintValidator<CompanyNameUniquePerWhitelabelId, CompanyInput> {
override fun isValid(companyInput: CompanyInput?, context: ConstraintValidatorContext?): Boolean {
// TODO
}
}
@MustBeDocumented
@Constraint(validatedBy = [Iso2CountryValidator::class])
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class IsIso2Country(
val message: String = "Wrong country code",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Any>> = []
)
class Iso2CountryValidator : ConstraintValidator<IsIso2Country, Int?> {
override fun isValid(value: Int?, context: ConstraintValidatorContext?): Boolean {
// TODO
}
} In my case, none of Any thoughts? Thanks! |
@Michal-Kucera this might be related to this issue: spring-projects/spring-graphql#624 which was fixed in this commit spring-projects/spring-graphql@581b110, and will be released with Spring GraphQL 1.1.3. |
Thanks for your fast response @koenpunt! Next time I should perhaps first look into graphql issues :) |
Nice issue request. Thanks, @Michal-Kucera |
Affects: 5.2.0.RC1
when using Kotlin suspend function and Validated annotation, like this.
occured exception.
The text was updated successfully, but these errors were encountered: