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

DocumentReference does not work (after updating to 3.2) when mongodb-driver-reactivestreams is a dependency #4629

Closed
caspianb opened this issue Jan 31, 2024 · 1 comment
Labels
for: external-project For an external project and not something we can fix

Comments

@caspianb
Copy link

caspianb commented Jan 31, 2024

I'm uncertain if this belongs here or in spring-boot since it's potentially a springboot-autoconfiguration project issue as well (I'm also uncertain if this should actually be considered an issue at all -- but since it was a bit of a pain to figure out the root cause of after updating to spring-boot 3.2 I figured I'd put it here).

The project in question uses two relevant dependencies:

    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.mongodb:mongodb-driver-reactivestreams'

The project, in general, uses non-reactive mongo; reactivestreams are pulled in to utilize for changestream registration only. The following field would simply get set to NULL when reading from the (non-reactive) mongoTemplate.

This issue only started occurring after updating -- likely just due to low level changes causing a change in autoconfiguration orderings. Entities with a @DecumentReference field setup in this manner would simply start returning NULL (no proxy, or anything, was being configured):

    @Setter(AccessLevel.NONE)
    @ReadOnlyProperty
    @DocumentReference(lookup = "{'_id': {$oid: ?#{#self.parentId}}}", lazy = true)
    private ParentDocument parent;

After debugging the issue, I noticed that the MappingMongoConverter being autowired into the MongoTemplate contains a DbRefResolver of NoOpDbRefResolver.

This is occurring (and this is where I am uncertain if this should be logged her or to spring-autoconfigure) because MongoReactiveDataAutoConfiguration is being configured before MongoDatabaseFactoryDependentConfiguration.

Both autoconfiguration classes contain configuration to create a MappingMongoConverter bean:

// in MongoDatabaseFactoryDependentConfiguration
@Bean
@ConditionalOnMissingBean(MongoConverter.class)
MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory factory, MongoMappingContext context,
        MongoCustomConversions conversions) {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
    MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
    mappingConverter.setCustomConversions(conversions);
    return mappingConverter;
}


// In MongoReactiveDataAutoConfiguration
@Bean
@ConditionalOnMissingBean(MongoConverter.class)
public MappingMongoConverter mappingMongoConverter(MongoMappingContext context,
        MongoCustomConversions conversions) {
    MappingMongoConverter mappingConverter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, context);
    mappingConverter.setCustomConversions(conversions);
    return mappingConverter;
}

I was able to work around the issue by copy-pasting the non-reactive bean definition above in my application's configuration.

The questions I have are:

  • should this be considered an issue at all? If so...
  • should the code be changed to ensure the MappingMongoConverter for non-reactive is configured first if both autoconfigurations are running?
  • should the code be changed to create a different mapping mongo converter for the non-reactive template from the reactive template?
  • does this belong here or in the root springboot project since the classes in question are autoconfiguration (I am uncertain who "owns" the code that lives in such places).
@caspianb caspianb changed the title DocumentReference does not work when mongodb-driver-reactivestreams is added as a dependency DocumentReference does not work (after updating to 3.2) when mongodb-driver-reactivestreams is a dependency Jan 31, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 31, 2024
@mp911de
Copy link
Member

mp911de commented Feb 5, 2024

This is a Spring Boot concern primarily. Previously, the synchronous configuration was applied first, and so the reference resolution was configured using the synchronous driver. While this isn't ideal in reactive scenarios, it was still working. We use a single converter bean, and therefore, we cannot distinguish between reactive and synchronous relation fetching.

It seems some change in Spring Boot switched the configuration order. I suggest filing a ticket at https://github.com/spring-projects/spring-boot/issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants