Skip to content
New issue

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

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

Already on GitHub? # to your account

MethodArgumentNotValidException not raised anymore for jakarta validation #40055

Closed
pepavesely opened this issue Mar 21, 2024 · 4 comments
Closed
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue

Comments

@pepavesely
Copy link

pepavesely commented Mar 21, 2024

In the version of Springboot 3.2.1, following worked:

  • class with jakarta.validation.constraints.Size annotation on a field
  • Java controller request body with given class annotated with jakarta.validation.Valid;
  • Class anotated with ControllerAdvice and @ExceptionHandler(MethodArgumentNotValidException.class) was handling validation exceptions

After spring-boot-starter upgrade to 3.2.2 (and also 3.2.3), MethodArgumentNotValidException is not raised anymore, HandlerMethodValidationException is raised instead.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 21, 2024
@wilkinsona
Copy link
Member

There were some notable changes in this area in Spring Framework 6.1 (spring-projects/spring-framework#29825). The change that you're seeing may be as a result of a refinement of those changes in a Spring Framework patch release. It's hard to be certain with the level of detail that you've currently provided. You could try using spring.version to override the version of Spring Framework that's used to see if this changes the behavior. If it does, then this will have to be investigated by the Spring Framework team. If it does not then we'll need some more information in the form of a minimal sample that we can use to reproduce the behavior you have described.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Mar 21, 2024
@scottfrederick
Copy link
Contributor

This comment on a Spring Framework issue might also be relevant.

@pepavesely
Copy link
Author

pepavesely commented Mar 25, 2024

Hi,
@scottfrederick thank you for the link. This issue seems to be duplicate of that one.

@PostMapping("/test1")
ResponseEntity<String> test1(@Valid @RequestBody TestModel testModel) {
    return ResponseEntity.ok().build();
}

throws MethodArgumentNotValidException but

@PostMapping("/test2/{param}")
ResponseEntity<String> test2(
        @NotNull @PathVariable(name = "param") String param,
        @Valid @RequestBody TestModel testModel) {
    return ResponseEntity.ok().build();
}

throws HandlerMethodValidationException

My pom.xml is fairly simple:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.4</version>
    <relativePath/>
</parent>

<properties>
    <java.version>17</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
   <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

TestApplication.class:

package com.example.test;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

@ControllerAdvice
@RestController
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @PostMapping("/test1")
    ResponseEntity<String> test1(@Valid @RequestBody TestModel testModel) {
        return ResponseEntity.ok().build();
    }

    @PostMapping("/test2/{param}")
    ResponseEntity<String> test2(
            @NotNull @PathVariable(name = "param") String param,
            @Valid @RequestBody TestModel testModel) {
        return ResponseEntity.ok().build();
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<String> handleException(final MethodArgumentNotValidException ex) {
        return ResponseEntity.badRequest().build();
    }

    @ExceptionHandler
    public ResponseEntity<String> handleUncaughtExceptions(final HandlerMethodValidationException ex) {
        return ResponseEntity.badRequest().build();
    }

    @Data
    public static class TestModel {

        @Size(min = 3)
        private String name;
    }
}

In spring-boot-starter-parent version 3.2.1, both cases are handled by MethodArgumentNotValidException

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 25, 2024
@wilkinsona
Copy link
Member

Thanks, @pepavesely. Closing as a duplicate of spring-projects/spring-framework#32396.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2024
@wilkinsona wilkinsona added status: duplicate A duplicate of another issue for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Mar 25, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants