Azure / Azure/azure-sdk-for-java
[BUG] Spring @DateTimeFormat(iso = ISO.DATE_TIME) not working with Cosmos DB
- 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**


**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
Assessment
This issue has not been assessed yet.