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

Method validation with @Validated not working [SPR-17369] #21902

Closed
spring-issuemaster opened this issue Oct 11, 2018 · 4 comments
Closed

Method validation with @Validated not working [SPR-17369] #21902

spring-issuemaster opened this issue Oct 11, 2018 · 4 comments
Assignees

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Oct 11, 2018

blaugold opened SPR-17369 and commented

I would like to use validation groups in my service layer to validate dtos, but am not able to trigger validation exceptions when using @Validated on a method parameter. @Valid works, but to use groups I need @Validated to work. I have isolated the issue in a reproduction repo which includes these minimal classes:

class Obj(

        @field:NotNull
        var default: String? = "",

        @field:NotNull(groups = [GroupA::class])
        var groupA: String? = ""

) {

    interface GroupA

    companion object {
        val Valid = Obj()
        val Invalid = Obj(null, null)
        val DefaultInvalid = Obj(null, "")
        val GroupAInvalid = Obj("", null)
    }

}

@Service
@Validated
class Service {

    fun plainMethod(obj: Obj) {}

    fun validMethod(@Valid obj: Obj) {}

    fun validatedMethod(@Validated obj: Obj) {}

    fun groupValidatedMethod(@Validated(Obj.GroupA::class) obj: Obj) {}

}

Service.validMethod throws as expected if obj is invalid (for default group constraints), but the methods where @Validated is used do not seem to validated obj at all.

I was not able to find any examples where @Validated is used in a service, only in controllers, but from the docs I got the impression this should work.


Affects: 5.0.9

Reference URL: https://github.com/blaugold/spring-method-validation-issue

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 12, 2018

Brian Clozel commented

Hi blaugold,

I may be mistaken, but I think that JSR 303 validation requires the annotation to be on the getter, not the field itself. Could you try that and let us know? See the Kotlin doc.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 12, 2018

blaugold commented

Hi Brian Clozel,

thanks for getting back to me. I tried it with get as the annotation use site with the same result. Validation of method parameters does work, but only when the parameter is annotated with @Valid and not @Validated. In the replication project are some tests which demonstrate that.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 17, 2018

Sébastien Deleuze commented

Based on a discussion I had with Juergen Hoeller, @Validated can only be used at the class level or method level but not at the individual parameter level. Putting @Validated(MyGroup.class) at the method level and then @Valid at each parameter with nested properties is correct. Essentially, @Validated triggers a Bean Validation validate step. For MethodValidationPostProcessor, this is always for the entire method, i.e. all parameters in one step. whereas in MVC, we validate individual parameters, with other parameters of the same method completely unaffected.

As a consequence I think current behavior is correct.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 17, 2018

blaugold commented

Thanks for investigating this issue. I got it working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.