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

Date saved incorrectly with java.time codecs enabled #4390

Closed
csmager opened this issue May 18, 2023 · 1 comment
Closed

Date saved incorrectly with java.time codecs enabled #4390

csmager opened this issue May 18, 2023 · 1 comment
Labels
type: bug A general bug

Comments

@csmager
Copy link

csmager commented May 18, 2023

I ran into this when I enabled java.time codecs on our Spring web application, as I'd like to save LocalDate as UTC rather than with the system default timezone.

I couldn't log into the app, and I chased this down to the MongoIndexedSessionRepository saving my session with an expiry date of midnight UTC this morning. This effectively meant there was no session, as they were created as expired.

I've now managed to get this down to a repro. It seems because there is a 'convertible pair' of Date and LocalDate, the conversion decides that it should convert Date to LocalDate, which is a lossy conversion.

I've tested with Spring Boot 2.7.12. My config looks like this:

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {

    @Override
    protected String getDatabaseName() {
        return "test";
    }

    @Override
    public MongoClient mongoClient() {
        return MongoClients.create("mongodb://localhost:27017/test");
    }

    @Override
    public MongoCustomConversions customConversions() {
        return MongoCustomConversions.create(
            MongoConverterConfigurationAdapter::useNativeDriverJavaTimeCodecs);
    }
}

And a simple test:

@SpringBootTest
public class DateTests {

    @Autowired
    private MongoOperations mongoOperations;

    @Test
    public void write_and_read_date() {
        var date = Date.from(Instant.now());

        var obj = new BasicDBObject("date", date);

        this.mongoOperations.save(obj, "test");

        var id = obj.get("_id");

        var result = this.mongoOperations.findById(id, BasicDBObject.class, "test");

        var resultDate = result.get("date");

        assertThat(resultDate).isEqualTo(date);
    }
}

That fails like this:

Expected: 2023-05-18T17:01:46.197 (java.util.Date)
Actual: 2023-05-18T01:00:00.000 (java.util.Date)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 18, 2023
@christophstrobl
Copy link
Member

Thanks for bringing this to our attention!
Right, useNativeDriverJavaTimeCodecs should prevent the Date to LocalDate conversion. We'll take care of that.

@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels May 22, 2023
mp911de pushed a commit that referenced this issue Jun 14, 2023
This commit prevents converters from being used as writing converter causing asymmetric write/read operations.

Closes #4390
Original pull request: #4392
mp911de pushed a commit that referenced this issue Jun 14, 2023
This commit prevents converters from being used as writing converter causing asymmetric write/read operations.

Closes #4390
Original pull request: #4392
mp911de pushed a commit that referenced this issue Jun 14, 2023
This commit prevents converters from being used as writing converter causing asymmetric write/read operations.

Closes #4390
Original pull request: #4392
@mp911de mp911de added this to the 3.4.13 (2021.2.13) milestone Jun 14, 2023
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.

4 participants