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

MethodArgumentNotValidException from spring-web requires spring-context to compile #29182

Closed
Andrius-B opened this issue Sep 21, 2022 · 4 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid

Comments

@Andrius-B
Copy link

Affects: org.springframework:spring-web:5.3.23

Trying to update from 5.2 to 5.3.23 lead us to an interesting issue, where a usage of MethodArgumentNotValidException would not compile. A minimal reproduction would be a gradle project as follows:
excerpt from build.gradle:

dependencies {
    implementation 'org.springframework:spring-web:5.3.23'
}

A java class:

import org.springframework.web.bind.MethodArgumentNotValidException;

public class Main {
    public static void main(String [] args) {
        throw new MethodArgumentNotValidException(null, null);
    }
}

This fails to compile with following error:

error: cannot access BindException
        throw new MethodArgumentNotValidException(null, null);
        ^
  class file for org.springframework.validation.BindException not found

This is because MethodArgumentNotValidException (from spring-web) extends org.springframework.validation.BindException (from spring-context), but the pom.xml that gradle sees does not specify this compile time dependency: https://search.maven.org/artifact/org.springframework/spring-web/5.3.23/jar
Only spring-beans and spring-core are specified as compile time dependencies (thus classes from these are present.)

This change of MethodArgumentNotValidException extending BindException was discussed in issues #23107 #26219
I think the correct behaviour here would be to mark spring-context as a compile time dependency for spring-web as that would play along with various build systems better.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 21, 2022
@sdeleuze
Copy link
Contributor

Reading #23282 and #23486 we could consider it is expected, but I am wondering if we should differentiate optional Spring dependencies like here spring-context which could potentially make sense to declare in the published pom.xml from external dependencies like the use cases described in #23234 where we should not as we have multiple confirmation this is a dead end. Any thoughts @bclozel ?

@bclozel
Copy link
Member

bclozel commented Sep 23, 2022

@sdeleuze I don't think this issue is related to the publishing of optional dependencies. This would not solve the point made by @Andrius-B as they still would get the compilation error in their project.

@Andrius-B spring-web ships lots of support for optional dependencies and we can't add them explicitly for obvious reasons. The validation and binding features from spring-web are used in the context of spring-webmvc or spring-webflux, and they both have a mandatory dependency on spring-context. One would argue that those features are not mandatory for using, for example, RestTemplate?

Could you elaborate on the original use case you were working on when encountering this problem?

@rstoyanchev rstoyanchev added status: waiting-for-feedback We need additional information before we can continue in: web Issues in web modules (web, webmvc, webflux, websocket) labels Oct 3, 2022
@Andrius-B
Copy link
Author

Maybe this is a not-so-common use-case where we have a package-based build system (bazel). For each package we declare the compile time dependencies for the source files in that package. We found a couple of cases where org.springframework.web.bind.MethodArgumentNotValidException was used without any other usages of spring-context. This causes causes a build error, though we have internally patched the declared dependencies of spring-web to include spring-context which fixes our issues. This is a slight inconvenience, but if publishing this dependency does not make sense in how the spring-web can be used, it's not a big problem.

Publishing the dependency as optional would indeed not fix our issue, but I initially thought this was an oversight and the issue should in fact be non-optional. Since @bclozel pointed out a case where the dependency is not needed, I will close this issue.
Thanks!

@bclozel bclozel added status: invalid An issue that we don't feel is valid and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 5, 2022
@bclozel
Copy link
Member

bclozel commented Oct 5, 2022

Thanks for letting us know @Andrius-B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants