Skip to content

Commit 1848684

Browse files
[Internal] Binary Encoding: Fixes DateTime Parsing Issue (#5133)
# Pull Request Template ## Description During some of the internal binary encoding opt-in customer workload validation, it was identified that when the cosmos .net SDK is initialized with binary encoding, and a date time field is persisted into a database, the value is getting converted and stored as in non-ISO format. Cosmos Client Creation: ``` Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, "True"); string databaseName = "binary-encoding-db"; string containerName = "binary-encoding-container"; CosmosClientOptions clientOptions = new() { ConnectionMode = ConnectionMode.Direct, RequestTimeout = TimeSpan.FromSeconds(5000), }; CosmosClient client = new CosmosClient(connectionString, clientOptions); ItemResponse<ToDoActivity> itemResponse = await container.CreateItemAsync(item); ``` Sample Data Saved in Container, when binary encoding is **Disabled** in the SDK: ![Image](https://github.com/user-attachments/assets/4e2e17cf-80fa-48d3-9e3a-3a2c7497ee2e) Sample Data Saved in Container, when binary encoding is **Enabled** in the SDK: ![Image](https://github.com/user-attachments/assets/e662e8af-821a-48a4-9744-c094fa801e48) This PR is fixing the above parsing gap. It appears that when a stream is sent to the backend in `Text` format, the backend does some formatting on the incoming stream, that converts `DateTime` fields into `ISO 8601` format and stores it. ![image](https://github.com/user-attachments/assets/7638d0de-ad83-4920-b431-3de405a7a04f) Per my analysis, when binary encoding is enabled, and the stream sent to backend is in Binary format, the backend doesn't do any extra formatting on the binary stream, and stores the stream as is. Therefore, [the round-trip ("O", "o") format specifier](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip) in the SDK layer is ideal to keep the `DateTime` field preserved in ISO standard, when binary encoding is enabled, ## Type of change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) ## Closing issues To automatically close an issue: closes #5132
1 parent e1ee560 commit 1848684

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

Microsoft.Azure.Cosmos/src/Json/Interop/CosmosDBToNewtonsoftWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public override void WriteValue(decimal value)
322322
/// <param name="value">The <see cref="DateTime"/> value to write.</param>
323323
public override void WriteValue(DateTime value)
324324
{
325-
this.WriteValue(value.ToString());
325+
this.WriteValue(value.ToString("O"));
326326
}
327327

328328
/// <summary>

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Json/NewtonsoftInteropTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void AllPrimitivesObjectTest()
202202
new JProperty("string", "XCPCFXPHHF"),
203203
new JProperty("boolean", true),
204204
new JProperty("null", null),
205-
new JProperty("datetime", "2526-07-11T18:18:16.4520716"),
205+
new JProperty("datetime", DateTime.Parse("2526-07-11T18:18:16.4520716")),
206206
new JProperty("spatialPoint", new JObject(
207207
new JProperty("type", "Point"),
208208
new JProperty("coordinate", new double[] { 118.9897, -46.6781 }))),

0 commit comments

Comments
 (0)