Azure / Azure/azure-sdk-for-java

[BUG] Spring @DateTimeFormat(iso = ISO.DATE_TIME) not working with Cosmos DB

Open
#34,849 11 comments 0 reactions 2 assignees Assigned to @jeet1995 View on GitHub
bug Client Cosmos cosmos-java-ecosystem-se-planning customer-reported needs-team-attention Service Attention
Dominant language
Java
Stars
2.6k
Forks
2.2k
Avg merge
2d 8h
Merged PRs (30d)
178

Description

**Describe the bug**
Despite expecting ISO-8601 string to be stored, the datetime is being stored as number, Applies for types LocalDateTime, Instant and ZonedDateTime and Date

***Exception or Stack Trace***
No exception thrown

**To Reproduce**

1. Create Model with ZonedDateTime property, using @DateTimeFormat(iso = ISO.DATE_TIME) annotation
1. Create Repository
1. Set spring boilerplate to create model instance and trigger save method of repository
1. Check Cosmos Explorer Items
Model
***Code Snippet***
```java
import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;
import java.time.ZonedDateTime;
import java.util.UUID;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

@Data
@Container(containerName = "FileMetaData")
public class MetaData {

@Id
UUID id;

String safeName;
String displayName;
String mimeType;
FileExtension extension;

@DateTimeFormat(iso = ISO.DATE)
ZonedDateTime createdAt = ZonedDateTime.now();

@PartitionKey
@DateTimeFormat(iso = ISO.DATE_TIME)
ZonedDateTime deletedAt;
}
```
Repository
```java
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Repository;

@Repository
public interface MetaDataRepository extends CosmosRepository { }
```

**Expected behavior**
Expected date to be stored as an ISO-8601 string

**Screenshots**
![image](https://github.com/Azure/azure-sdk-for-java/assets/15959001/eeaaa23b-9666-4fd5-9773-7980fed2e46e)
![image](https://github.com/Azure/azure-sdk-for-java/assets/15959001/75666de7-b2ac-44ff-b4a0-b6b9e625a483)

**Setup (please complete the following information):**
- OS: Windows 10
- IDE: IntelliJ
- Library/Libraries: spring-cloud-azure-starter-data-cosmos:4.7.0 (com.azure.spring)
- Java version: 17
- App Server/Environment: Tomcat
- Frameworks: Spring Boot 2.7.10, Cosmos Emulator 2.14.12.0

If you suspect a dependency version mismatch (e.g. you see `NoClassDefFoundError`, `NoSuchMethodError` or similar), please check out [Troubleshoot dependency version conflict article](https://aka.ms/azsdk/java/dependency/troubleshoot) first. If it doesn't provide solution for the problem, please provide:
- verbose dependency tree (`mvn dependency:tree -Dverbose`)
- exception message, full stack trace, and any available logs
NA

**Additional context**
NA

**Information Checklist**
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [X] Bug Description Added
- [X] Repro Steps Added
- [X] Setup information Added

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.