Skip to content

Conversation

@JGoP-L
Copy link
Contributor

@JGoP-L JGoP-L commented Nov 17, 2025

Fix MongoDB index creation API compatibility across Spring Data MongoDB versions

Problem

Spring AI 1.1.0 fails to start when spring.ai.chat.memory.repository.mongo.create-indices=true is configured with Spring Boot 3.4.x, throwing:

java.lang.NoSuchMethodError: 'java.lang.String org.springframework.data.mongodb.core.index.IndexOperations.createIndex(org.springframework.data.mongodb.core.index.IndexDefinition)'

Root Cause:

Spring Data MongoDB changed its index creation API between versions:

  • 4.2.x - 4.4.x (Spring Boot 3.2.x - 3.4.x): Only ensureIndex(IndexDefinition) is available
  • 4.5.x+ (Spring Boot 3.5.x+): createIndex(IndexDefinition) is the new API, ensureIndex is deprecated

The original code in MongoChatMemoryIndexCreatorAutoConfiguration hardcoded createIndex(), which doesn't exist in MongoDB 4.2.x - 4.4.x, causing runtime errors for users on officially supported Spring Boot versions.

Solution

Implemented a reflection-based fallback strategy to dynamically choose the correct method at runtime:

  1. Try createIndex() first (for Spring Data MongoDB 4.5.x+)
  2. Fall back to ensureIndex() if NoSuchMethodException occurs (for 4.2.x - 4.4.x)
  3. Throw IllegalStateException with clear error message if neither method is available

This approach follows Spring AI's existing pattern for handling optional dependencies (see SpringAiRetryAutoConfiguration).

Testing

Successfully tested across multiple Spring Boot versions:

Spring Boot Spring Data MongoDB Method Used Result
3.2.4 4.2.4 ensureIndex() ✅ Pass
3.3.4 4.3.4 ensureIndex() ✅ Pass
3.4.1 4.4.1 ensureIndex() ✅ Pass
3.5.4 4.5.2 createIndex() ✅ Pass

Test command:

mvn clean test -pl auto-configurations/models/chat/memory/repository/spring-ai-autoconfigure-model-chat-memory-repository-mongodb

Impact

  • Backward compatible: Works with Spring Data MongoDB 4.2.x - 4.4.x
  • Forward compatible: Works with Spring Data MongoDB 4.5.x+
  • No breaking changes: Existing functionality preserved
  • Minimal performance impact: Reflection only executed twice during application startup
  • Clear error handling: Provides actionable error messages if unsupported MongoDB version is detected

Related Issues

Fixes #4884


Checklist

  • I have signed the DCO with Signed-off-by line
  • My changes are rebased on the latest main branch
  • My commits are squashed into a single commit
  • Code follows Spring AI coding standards (reflection pattern consistent with SpringAiRetryAutoConfiguration)
  • Tests pass locally across Spring Boot 3.2.x - 3.5.x
  • No new dependencies added
  • JavaDoc updated to explain the API version differences

@JGoP-L JGoP-L force-pushed the fix/issue-4884-mongodb-index-api-compatibility branch from 6c055ac to ab96e9f Compare November 17, 2025 02:43
}
catch (ReflectiveOperationException ex) {
String message = "Failed to invoke ensureIndex() method";
logger.error(message, ex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the message to the logger call, no need to create and additional object, same above and below

…DB versions

Use reflection to handle API differences between Spring Data MongoDB versions:
- Spring Data MongoDB 4.2.x - 4.4.x: only ensureIndex() is available
- Spring Data MongoDB 4.5.x+: createIndex() is the new API, ensureIndex is deprecated

This fix ensures compatibility with both Spring Boot 3.4.x (MongoDB 4.4.x) and
Spring Boot 3.5.x (MongoDB 4.5.x+), which are both officially supported versions.

Fixes spring-projects#4884

Signed-off-by: shaojie <741047428@qq.com>
@JGoP-L JGoP-L requested a review from deejay1 November 17, 2025 13:55
@JGoP-L JGoP-L force-pushed the fix/issue-4884-mongodb-index-api-compatibility branch from d0d14d5 to c7a9e4b Compare November 17, 2025 13:55
@ilayaperumalg ilayaperumalg added this to the 1.1.1 milestone Nov 17, 2025
@ilayaperumalg ilayaperumalg self-assigned this Nov 18, 2025
@ilayaperumalg
Copy link
Member

@JGoP-L Thanks for the PR fixing the issue and @deejay1 thanks for the review as well!

Rebased and merged into main via e87b2ba. Also, cherry-picked into 1.1.x via 0821f48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB Chat Memory Repository Application - APPLICATION FAILED TO START when **create-indices = true**

3 participants