Skip to content

MongoClientVersion uses a non-reliable way to fetch db driver version. #4937

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

Closed
smagellan opened this issue Apr 7, 2025 · 1 comment
Closed
Assignees
Labels
type: bug A general bug

Comments

@smagellan
Copy link

smagellan commented Apr 7, 2025

Hello.

MongoClientVersion does its best to determine mongodb driver version properly and use a proper driver API.
But looks like com.mongodb.internal.build.MongoDriverVersion.VERSION value is inlined and always 5.2.1 (for spring-data-mongodb 4.4.4), note LDC "5.2.1":

Bytecode
private static getVersionFromPackage(Ljava/lang/ClassLoader;)Lorg/springframework/data/util/Version;
    // parameter  classLoader
  @Lorg/springframework/lang/Nullable;()
    TRYCATCHBLOCK L0 L1 L2 java/lang/IllegalArgumentException
   L3
    LINENUMBER 97 L3
    LDC "com.mongodb.internal.build.MongoDriverVersion"
    ALOAD 0
    INVOKESTATIC org/springframework/util/ClassUtils.isPresent (Ljava/lang/String;Ljava/lang/ClassLoader;)Z
    IFEQ L4
   L0
    LINENUMBER 99 L0
    LDC "5.2.1"
    INVOKESTATIC org/springframework/data/util/Version.parse (Ljava/lang/String;)Lorg/springframework/data/util/Version;
   L1
    ARETURN
   L2
    LINENUMBER 100 L2
   FRAME SAME1 java/lang/IllegalArgumentException
    ASTORE 1
   L4
    LINENUMBER 104 L4
   FRAME SAME
    ACONST_NULL
    ARETURN
   L5
    LOCALVARIABLE classLoader Ljava/lang/ClassLoader; L3 L5 0
    MAXSTACK = 2
    MAXLOCALS = 2
This will not work properly if one e.g. excludes driver 5.X version and provides 4.X explicitly instead, see stacktrace:
Stacktrace
Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.springframework.data.mongodb.core.MongoTemplate.lambda$getCollectionNames$29(MongoTemplate.java:2353)
	at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:582)
	at org.springframework.data.mongodb.core.MongoTemplate.getCollectionNames(MongoTemplate.java:2351)
	at smagellan.test.mongodb.SpringDataMongodbRunner.main(SpringDataMongodbRunner.java:15)
Caused by: java.lang.IllegalStateException: Unable to load com.mongodb.client.ListCollectionNamesIterable
	at org.springframework.data.mongodb.util.MongoCompatibilityAdapter$MongoDatabaseAdapter.<clinit>(MongoCompatibilityAdapter.java:228)
	... 4 more
Caused by: java.lang.ClassNotFoundException: com.mongodb.client.ListCollectionNamesIterable
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:534)
	at java.base/java.lang.Class.forName(Class.java:513)
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:299)
	at org.springframework.data.mongodb.util.MongoCompatibilityAdapter$MongoDatabaseAdapter.<clinit>(MongoCompatibilityAdapter.java:225)
	... 4 more
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 7, 2025
smagellan added a commit to smagellan/spring-data-mongodb that referenced this issue Apr 7, 2025
Dynamically load com.mongodb.internal.build.MongoDriverVersion.VERSION
--
Closes spring-projectsgh-4937

Signed-off-by: Vladimir Vishnevskii <[email protected]>

Signed-off-by: Vladimir Vishnevskii <[email protected]>
@smagellan smagellan changed the title MongoClientVersion might use a non-reliable way to fetch db driver version. MongoClientVersion uses a non-reliable way to fetch db driver version. Apr 8, 2025
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 9, 2025
@mp911de mp911de self-assigned this Apr 9, 2025
@mp911de
Copy link
Member

mp911de commented Apr 9, 2025

Good catch. Please note that we will remove Driver version 4 compatibility with the next major release.

mp911de added a commit that referenced this issue Apr 9, 2025
To avoid inlining of the final/static version value, we're using reflection to look up the version value.

Closes #4937
mp911de added a commit that referenced this issue Apr 9, 2025
To avoid inlining of the final/static version value, we're using reflection to look up the version value.

Closes #4937
mp911de added a commit that referenced this issue Apr 9, 2025
To avoid inlining of the final/static version value, we're using reflection to look up the version value.

Closes #4937
@mp911de mp911de closed this as completed in 6f11c08 Apr 9, 2025
@mp911de mp911de added this to the 4.3.11 (2024.0.11) milestone Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants