Skip to content

Commit 261e070

Browse files
Refactors JSON property names into shared constant and changes etag constant
Extract all JSON field name string literals from DistributedTransactionSerializer into `internal const string` constants at the top of the class Replace the literals in the serializer body with the new constants. Update all test files that previously hardcoded these same strings in `GetProperty`/`TryGetProperty` calls to reference `DistributedTransactionSerializer.<Constant>` instead — ensuring any future rename of a property automatically propagates to tests without a separate search-and-replace: - `DistributedTransactionOperationResult.cs` - `DistributedTransactionSerializerTests.cs` - `DistributedTransactionCommitterTests.cs` - `DistributedTransactionTests.cs` (emulator) No behavioral changes. All 58 unit tests pass. Additionally, updates the constant `etag` to `ifMatch` in the request body to align with the wire contract update.
1 parent 9361602 commit 261e070

5 files changed

Lines changed: 174 additions & 153 deletions

File tree

Microsoft.Azure.Cosmos/src/DistributedTransaction/DistributedTransactionOperationResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ internal static DistributedTransactionOperationResult FromJson(JsonElement json)
140140
{
141141
DistributedTransactionOperationResult result = JsonSerializer.Deserialize<DistributedTransactionOperationResult>(json, DistributedTransactionOperationResult.CaseInsensitiveOptions);
142142

143-
if (json.TryGetProperty("resourceBody", out JsonElement resourceBody)
143+
if (json.TryGetProperty(DistributedTransactionSerializer.ResourceBody, out JsonElement resourceBody)
144144
&& resourceBody.ValueKind != JsonValueKind.Undefined
145145
&& resourceBody.ValueKind != JsonValueKind.Null)
146146
{

Microsoft.Azure.Cosmos/src/DistributedTransaction/DistributedTransactionSerializer.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ namespace Microsoft.Azure.Cosmos
1616
/// </summary>
1717
internal static class DistributedTransactionSerializer
1818
{
19+
internal const string Operations = "operations";
20+
internal const string DatabaseName = "databaseName";
21+
internal const string CollectionName = "collectionName";
22+
internal const string Id = "id";
23+
internal const string CollectionResourceId = "collectionResourceId";
24+
internal const string DatabaseResourceId = "databaseResourceId";
25+
internal const string PartitionKey = "partitionKey";
26+
internal const string Index = "index";
27+
internal const string ResourceBody = "resourceBody";
28+
internal const string SessionToken = "sessionToken";
29+
internal const string ETag = "ifMatch";
30+
internal const string OperationType = "operationType";
31+
internal const string ResourceType = "resourceType";
32+
1933
/// <summary>
2034
/// Serializes a distributed transaction request body to a JSON stream.
2135
/// The body contains only the operations array. Other metadata like idempotencyToken,
@@ -32,7 +46,7 @@ public static MemoryStream SerializeRequest(IReadOnlyList<DistributedTransaction
3246
jsonWriter.WriteStartObject();
3347

3448
// operations array
35-
jsonWriter.WriteStartArray("operations");
49+
jsonWriter.WriteStartArray(Operations);
3650

3751
foreach (DistributedTransactionOperation operation in operations)
3852
{
@@ -58,30 +72,30 @@ private static void WriteOperation(Utf8JsonWriter jsonWriter, DistributedTransac
5872
jsonWriter.WriteStartObject();
5973

6074
// databaseName
61-
jsonWriter.WriteString("databaseName", operation.Database);
75+
jsonWriter.WriteString(DatabaseName, operation.Database);
6276

6377
// collectionName
64-
jsonWriter.WriteString("collectionName", operation.Container);
78+
jsonWriter.WriteString(CollectionName, operation.Container);
6579

6680
// id
67-
jsonWriter.WriteString("id", operation.Id);
81+
jsonWriter.WriteString(Id, operation.Id);
6882

6983
// collectionResourceId
7084
if (operation.CollectionResourceId != null)
7185
{
72-
jsonWriter.WriteString("collectionResourceId", operation.CollectionResourceId);
86+
jsonWriter.WriteString(CollectionResourceId, operation.CollectionResourceId);
7387
}
7488

7589
// databaseResourceId
7690
if (operation.DatabaseResourceId != null)
7791
{
78-
jsonWriter.WriteString("databaseResourceId", operation.DatabaseResourceId);
92+
jsonWriter.WriteString(DatabaseResourceId, operation.DatabaseResourceId);
7993
}
8094

8195
// partitionKey
8296
if (operation.PartitionKeyJson != null)
8397
{
84-
jsonWriter.WritePropertyName("partitionKey");
98+
jsonWriter.WritePropertyName(PartitionKey);
8599
jsonWriter.WriteRawValue(operation.PartitionKeyJson, skipInputValidation: true);
86100
}
87101

@@ -90,32 +104,31 @@ private static void WriteOperation(Utf8JsonWriter jsonWriter, DistributedTransac
90104
{
91105
throw new ArgumentOutOfRangeException(nameof(operation.OperationIndex), "Operation index must be non-negative.");
92106
}
93-
jsonWriter.WriteNumber("index", (uint)operation.OperationIndex);
107+
jsonWriter.WriteNumber(Index, (uint)operation.OperationIndex);
94108

95109
//resourceBody - written as nested JSON object
96110
if (!operation.ResourceBody.IsEmpty)
97111
{
98-
jsonWriter.WritePropertyName("resourceBody");
112+
jsonWriter.WritePropertyName(ResourceBody);
99113
jsonWriter.WriteRawValue(operation.ResourceBody.Span, skipInputValidation: true);
100114
}
101115

102116
// sessionToken
103117
if (operation.SessionToken != null)
104118
{
105-
jsonWriter.WriteString("sessionToken", operation.SessionToken);
119+
jsonWriter.WriteString(SessionToken, operation.SessionToken);
106120
}
107121

108122
// etag
109123
if (operation.ETag != null)
110124
{
111-
jsonWriter.WriteString("etag", operation.ETag);
125+
jsonWriter.WriteString(ETag, operation.ETag);
112126
}
113127

114128
// operationType (string)
115-
jsonWriter.WriteString("operationType", operation.OperationType.ToString());
116-
129+
jsonWriter.WriteString(OperationType, operation.OperationType.ToString());
117130
// resourceType (string)
118-
jsonWriter.WriteString("resourceType", ResourceType.Document.ToString());
131+
jsonWriter.WriteString(ResourceType, Documents.ResourceType.Document.ToString());
119132

120133
jsonWriter.WriteEndObject();
121134
}

0 commit comments

Comments
 (0)