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

How to configure custom BsonTypeClassMap #59

Open
XSpielinbox opened this issue Sep 8, 2024 · 1 comment
Open

How to configure custom BsonTypeClassMap #59

XSpielinbox opened this issue Sep 8, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@XSpielinbox
Copy link

Unfortunately, MongoDB by default decodes BSON DateTime to java.util.Date, not java.time.LocalDateTime, which causes all sorts of errors in our application.

To circumvent this, we have the following class in our code base:

@Configuration
public class MongoConfig {

    @Bean
    public MongoClientSettingsBuilderCustomizer mongoClientSettingsBuilderCustomizer() {
        BsonTypeClassMap classMap = new BsonTypeClassMap(Map.of(BsonType.DATE_TIME, LocalDateTime.class));
        return builder -> builder.codecRegistry(CodecRegistries.fromRegistries(
                CodecRegistries.fromProviders(new DocumentCodecProvider(classMap)),
                MongoClientSettings.getDefaultCodecRegistry(),
                CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build())
        ));
    }
}

However, in the test classes, this does not get called and we still have these issues. We could not find a way how to configure this in Embedded Mongo directly or how to hand over custom MongoClientSettings. Unfortunately, when the ReactiveClientAdapter creates the MongoClient it sets the MongoClientSettings to the defaults which cannot be changed after the construction of the client.

We are using Spring Boot 3.3.3 with de.flapdoodle.embed.mongo version 4.17.0 and de.flapdoodle.embed.mongo.spring3x version 4.16.1 with Java 21.

How can we configure Embedded Mongo to not use the obsolete java.util.Date?

If you need any further information, please let me know.

Thank you for your great work and help!

@michaelmosmann
Copy link
Member

@XSpielinbox i will have a look at this..

@michaelmosmann michaelmosmann self-assigned this Sep 8, 2024
@michaelmosmann michaelmosmann added the bug Something isn't working label Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants