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.
Comment From: sdeleuze
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 ?
Comment From: bclozel
@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?
Comment From: Andrius-B
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!
Comment From: bclozel
Thanks for letting us know @Andrius-B