From a1738094c3e8bb4fe485d08c3ff8de2d27e6481f Mon Sep 17 00:00:00 2001 From: pure-zero Date: Wed, 25 Nov 2026 12:12:28 +1300 Subject: [PATCH 1/2] fix: Cosmos patch requests not using the custom serializer for operations when set --- .../ItemOperationPatchSerializerTest.java | 104 +++++++++++++ .../implementation/patch/PatchUnitTest.java | 143 ++++++++++++++++++ sdk/cosmos/azure-cosmos/CHANGELOG.md | 1 + .../batch/CosmosItemOperationBase.java | 11 ++ .../batch/ItemBatchOperation.java | 6 +- .../batch/ItemBulkOperation.java | 6 +- .../implementation/patch/PatchUtil.java | 51 ++++++- 7 files changed, 313 insertions(+), 9 deletions(-) create mode 100644 sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/batch/ItemOperationPatchSerializerTest.java diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/batch/ItemOperationPatchSerializerTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/batch/ItemOperationPatchSerializerTest.java new file mode 100644 index 0000000000000..cba55576f6cb7 --- /dev/null +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/batch/ItemOperationPatchSerializerTest.java @@ -0,0 +1,104 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.cosmos.implementation.batch; + +import com.azure.cosmos.CosmosItemSerializer; +import com.azure.cosmos.implementation.JsonSerializable; +import com.azure.cosmos.implementation.RequestOptions; +import com.azure.cosmos.models.CosmosItemOperationType; +import com.azure.cosmos.models.CosmosPatchOperations; +import com.azure.cosmos.models.PartitionKey; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.testng.annotations.Test; + +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Verifies that patch operations executed as part of a {@link com.azure.cosmos.models.CosmosBatch} or + * bulk ({@link com.azure.cosmos.models.CosmosBulkOperations}) request honor the effective item serializer + * that was resolved for the batch/bulk request (merged from the per-operation/batch-level request options + * and the client-level default custom serializer), rather than silently falling back to the default serializer. + */ +public class ItemOperationPatchSerializerTest { + + private static final String RESOURCE_BODY = "resourceBody"; + private static final String OPERATIONS = "operations"; + private static final String VALUE = "value"; + + @Test(groups = { "unit" }) + public void itemBatchOperationPatchUsesEffectiveItemSerializer() { + CosmosPatchOperations patchOperations = CosmosPatchOperations.create().replace("/foo", "originalValue"); + RequestOptions requestOptions = new RequestOptions(); + + ItemBatchOperation operation = new ItemBatchOperation<>( + CosmosItemOperationType.PATCH, + "id1", + PartitionKey.NONE, + requestOptions, + patchOperations + ); + + JsonSerializable serialized = operation.getSerializedOperation(new MarkerCosmosItemSerializer()); + + ObjectNode patchValue = extractPatchValueNode(serialized); + assertThat(patchValue.get("markerApplied").asBoolean()).isTrue(); + assertThat(patchValue.get("rawValue").asText()).isEqualTo("originalValue"); + + assertThat(requestOptions.getEffectiveItemSerializer()) + .isSameAs(CosmosItemSerializer.DEFAULT_SERIALIZER) + .isNotInstanceOf(MarkerCosmosItemSerializer.class); + assertThat(operation.getRequestOptions()).isSameAs(requestOptions); + } + + @Test(groups = { "unit" }) + public void itemBulkOperationPatchUsesEffectiveItemSerializer() { + CosmosPatchOperations patchOperations = CosmosPatchOperations.create().replace("/foo", "originalValue"); + RequestOptions requestOptions = new RequestOptions(); + + ItemBulkOperation operation = new ItemBulkOperation<>( + CosmosItemOperationType.PATCH, + "id1", + PartitionKey.NONE, + requestOptions, + patchOperations, + null + ); + + JsonSerializable serialized = operation.getSerializedOperation(new MarkerCosmosItemSerializer()); + + ObjectNode patchValue = extractPatchValueNode(serialized); + assertThat(patchValue.get("markerApplied").asBoolean()).isTrue(); + assertThat(patchValue.get("rawValue").asText()).isEqualTo("originalValue"); + + assertThat(requestOptions.getEffectiveItemSerializer()) + .isSameAs(CosmosItemSerializer.DEFAULT_SERIALIZER) + .isNotInstanceOf(MarkerCosmosItemSerializer.class); + assertThat(operation.getRequestOptions()).isSameAs(requestOptions); + } + + private static ObjectNode extractPatchValueNode(JsonSerializable serializedOperation) { + ObjectNode resourceBody = (ObjectNode) serializedOperation.getPropertyBag().get(RESOURCE_BODY); + ArrayNode operations = (ArrayNode) resourceBody.get(OPERATIONS); + return (ObjectNode) operations.get(0).get(VALUE); + } + + private static class MarkerCosmosItemSerializer extends CosmosItemSerializer { + @Override + public Map serialize(T item) { + Map map = new LinkedHashMap<>(); + map.put("markerApplied", true); + map.put("rawValue", item); + return map; + } + + @Override + public T deserialize(Map jsonNodeMap, Class classType) { + throw new UnsupportedOperationException(); + } + } +} diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java index 67a2940aafbf1..9faf34a9ce580 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java @@ -3,12 +3,19 @@ package com.azure.cosmos.implementation.patch; +import com.azure.cosmos.CosmosItemSerializer; +import com.azure.cosmos.implementation.ImplementationBridgeHelpers; +import com.azure.cosmos.implementation.JsonSerializable; +import com.azure.cosmos.implementation.RequestOptions; import com.azure.cosmos.models.CosmosPatchOperations; import com.azure.cosmos.models.ModelBridgeInternal; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.testng.annotations.Test; import java.time.Instant; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Random; import java.util.UUID; @@ -71,6 +78,142 @@ public void constructPatchOperation() { PatchUnitTest.validateOperations(cosmosPatchOperations, PatchOperationType.INCREMENT, value); } + @Test(groups = { "unit" }) + public void patchOperationValueUsesEffectiveCustomItemSerializer() { + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); + + RequestOptions requestOptions = new RequestOptions(); + requestOptions.setEffectiveItemSerializer(new MarkerCosmosItemSerializer()); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation(cosmosPatchOperations, requestOptions); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).isObject()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).get("markerApplied").asBoolean()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).get("rawValue").asText()).isEqualTo("originalValue"); + } + + @Test(groups = { "unit" }) + public void patchOperationValueFallsBackToDefaultSerializerWhenNoCustomSerializerConfigured() { + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); + + RequestOptions requestOptions = new RequestOptions(); + requestOptions.setEffectiveItemSerializer(null); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation(cosmosPatchOperations, requestOptions); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).asText()).isEqualTo("originalValue"); + } + + @Test(groups = { "unit" }) + public void patchOperationValueWorksWithNullRequestOptions() { + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation(cosmosPatchOperations, null); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).asText()).isEqualTo("originalValue"); + } + + @Test(groups = { "unit" }) + public void patchOperationValueUsesExplicitEffectiveItemSerializerOverload() { + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); + + RequestOptions requestOptions = new RequestOptions(); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation( + cosmosPatchOperations, requestOptions, new MarkerCosmosItemSerializer()); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).isObject()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).get("markerApplied").asBoolean()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).get("rawValue").asText()).isEqualTo("originalValue"); + + assertThat(requestOptions.getEffectiveItemSerializer()) + .isSameAs(CosmosItemSerializer.DEFAULT_SERIALIZER) + .isNotInstanceOf(MarkerCosmosItemSerializer.class); + } + + @Test(groups = { "unit" }) + public void explicitEffectiveItemSerializerOverloadIsIndependentOfRequestOptions() { + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); + + RequestOptions requestOptions = new RequestOptions(); + requestOptions.setEffectiveItemSerializer(new OtherMarkerCosmosItemSerializer()); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation( + cosmosPatchOperations, requestOptions, new MarkerCosmosItemSerializer()); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).get("markerApplied").asBoolean()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).has("otherMarkerApplied")).isFalse(); + } + + @Test(groups = { "unit" }) + public void patchOperationValueFallsBackToDefaultSerializerWhenCandidateCannotSerialize() { + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); + + MarkerCosmosItemSerializer candidate = new MarkerCosmosItemSerializer(); + ImplementationBridgeHelpers.CosmosItemSerializerHelper.getCosmosItemSerializerAccessor() + .setCanSerialize(candidate, false); + + RequestOptions requestOptions = new RequestOptions(); + requestOptions.setEffectiveItemSerializer(candidate); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation(cosmosPatchOperations, requestOptions); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).asText()).isEqualTo("originalValue"); + } + + @Test(groups = { "unit" }) + public void explicitEffectiveItemSerializerFallsBackToDefaultWhenCandidateCannotSerialize() { + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); + + MarkerCosmosItemSerializer candidate = new MarkerCosmosItemSerializer(); + ImplementationBridgeHelpers.CosmosItemSerializerHelper.getCosmosItemSerializerAccessor() + .setCanSerialize(candidate, false); + + RequestOptions requestOptions = new RequestOptions(); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation( + cosmosPatchOperations, requestOptions, candidate); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).asText()).isEqualTo("originalValue"); + } + + private static class MarkerCosmosItemSerializer extends CosmosItemSerializer { + @Override + public Map serialize(T item) { + Map map = new LinkedHashMap<>(); + map.put("markerApplied", true); + map.put("rawValue", item); + return map; + } + + @Override + public T deserialize(Map jsonNodeMap, Class classType) { + throw new UnsupportedOperationException(); + } + } + + private static class OtherMarkerCosmosItemSerializer extends CosmosItemSerializer { + @Override + public Map serialize(T item) { + Map map = new LinkedHashMap<>(); + map.put("otherMarkerApplied", true); + map.put("rawValue", item); + return map; + } + + @Override + public T deserialize(Map jsonNodeMap, Class classType) { + throw new UnsupportedOperationException(); + } + } + private static void validateOperations(CosmosPatchOperations cosmosPatchOperations, PatchOperationType operationType, T value) { List patchOperations = ModelBridgeInternal.getPatchOperationsFromCosmosPatch(cosmosPatchOperations); assertThat(patchOperations.size()).isEqualTo(1); diff --git a/sdk/cosmos/azure-cosmos/CHANGELOG.md b/sdk/cosmos/azure-cosmos/CHANGELOG.md index 27ed8439dfde3..9d9a6eb88ae08 100644 --- a/sdk/cosmos/azure-cosmos/CHANGELOG.md +++ b/sdk/cosmos/azure-cosmos/CHANGELOG.md @@ -8,6 +8,7 @@ #### Breaking Changes #### Bugs Fixed +* Fixed `CosmosPatchOperations` values not honoring the configured custom `CosmosItemSerializer` for single-item patch, `CosmosBatch`, and bulk patch operations, instead always falling back to the internal default serializer. * Fixed compound session token handling for requests routed to GatewayV2. - See [PR 51102](https://github.com/Azure/azure-sdk-for-java/pull/51102) * Fixed concurrent response parsing throwing decoder state exceptions when fallback UTF-8 decoding is enabled. - See [Issue 51084](https://github.com/Azure/azure-sdk-for-java/issues/51084) and [PR 51151](https://github.com/Azure/azure-sdk-for-java/pull/51151) * Fixed hybrid search reciprocal rank fusion (RRF) results being ranked incorrectly when component scores are tied. Each result now receives a unique ordinal rank, with deterministic tie-breaking based on the decoded document ID portion of `_rid`. - See [PR 50940](https://github.com/Azure/azure-sdk-for-java/pull/50940) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/CosmosItemOperationBase.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/CosmosItemOperationBase.java index ebc68ea5aca24..08588369daf41 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/CosmosItemOperationBase.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/CosmosItemOperationBase.java @@ -5,7 +5,10 @@ import com.azure.cosmos.CosmosItemSerializer; import com.azure.cosmos.implementation.JsonSerializable; +import com.azure.cosmos.implementation.RequestOptions; +import com.azure.cosmos.implementation.patch.PatchUtil; import com.azure.cosmos.models.CosmosItemOperation; +import com.azure.cosmos.models.CosmosPatchOperations; import java.util.concurrent.atomic.AtomicReference; @@ -42,4 +45,12 @@ private int getSerializedLengthInternal(CosmosItemSerializer effectiveItemSerial String serializedValue = operationSerializable.toString(); return serializedValue.codePointCount(0, serializedValue.length()); } + + static JsonSerializable getSerializedPatchOperationBody( + CosmosPatchOperations patchOperations, + RequestOptions requestOptions, + CosmosItemSerializer effectiveItemSerializer) { + + return PatchUtil.serializableBatchPatchOperation(patchOperations, requestOptions, effectiveItemSerializer); + } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBatchOperation.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBatchOperation.java index 63ef403f4be4c..5c415a130bf10 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBatchOperation.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBatchOperation.java @@ -7,7 +7,6 @@ import com.azure.cosmos.implementation.JsonSerializable; import com.azure.cosmos.implementation.RequestOptions; import com.azure.cosmos.implementation.apachecommons.lang.StringUtils; -import com.azure.cosmos.implementation.patch.PatchUtil; import com.azure.cosmos.models.CosmosItemOperationType; import com.azure.cosmos.models.CosmosPatchOperations; import com.azure.cosmos.models.ModelBridgeInternal; @@ -80,7 +79,10 @@ JsonSerializable getSerializedOperationInternal(CosmosItemSerializer effectiveIt if (this.getOperationType() == CosmosItemOperationType.PATCH) { jsonSerializable.set( BatchRequestResponseConstants.FIELD_RESOURCE_BODY, - PatchUtil.serializableBatchPatchOperation((CosmosPatchOperations) this.getItemInternal(), this.getRequestOptions()) + getSerializedPatchOperationBody( + (CosmosPatchOperations) this.getItemInternal(), + this.getRequestOptions(), + effectiveItemSerializer) ); } else { jsonSerializable.set( diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBulkOperation.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBulkOperation.java index b63b5b9c8e1d8..ac2e38df1887a 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBulkOperation.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/ItemBulkOperation.java @@ -9,7 +9,6 @@ import com.azure.cosmos.implementation.JsonSerializable; import com.azure.cosmos.implementation.RequestOptions; import com.azure.cosmos.implementation.apachecommons.lang.StringUtils; -import com.azure.cosmos.implementation.patch.PatchUtil; import com.azure.cosmos.models.CosmosItemOperationType; import com.azure.cosmos.models.CosmosPatchOperations; import com.azure.cosmos.models.ModelBridgeInternal; @@ -109,9 +108,10 @@ JsonSerializable getSerializedOperationInternal(CosmosItemSerializer effectiveIt if (this.getOperationType() == CosmosItemOperationType.PATCH) { jsonSerializable.set( BatchRequestResponseConstants.FIELD_RESOURCE_BODY, - PatchUtil.serializableBatchPatchOperation( + getSerializedPatchOperationBody( (CosmosPatchOperations) this.getItemInternal(), - this.getRequestOptions()), + this.getRequestOptions(), + effectiveItemSerializer), internalDefaultSerializer(), false); } else { diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java index fef98d681a0a9..31bb2b562f559 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java @@ -3,6 +3,9 @@ package com.azure.cosmos.implementation.patch; +import com.azure.cosmos.CosmosItemSerializer; +import com.azure.cosmos.implementation.ImplementationBridgeHelpers; +import com.azure.cosmos.implementation.ImplementationBridgeHelpers.CosmosItemSerializerHelper.CosmosItemSerializerAccessor; import com.azure.cosmos.implementation.JsonSerializable; import com.azure.cosmos.implementation.RequestOptions; import com.azure.cosmos.implementation.Utils; @@ -14,12 +17,25 @@ public final class PatchUtil { + private static CosmosItemSerializerAccessor cosmosItemSerializerAccessor() { + return ImplementationBridgeHelpers.CosmosItemSerializerHelper.getCosmosItemSerializerAccessor(); + } + public static JsonSerializable serializableBatchPatchOperation(CosmosPatchOperations patchOperations, RequestOptions requestOptions) { - return cosmosPatchToJsonSerializable(patchOperations, requestOptions); + return cosmosPatchToJsonSerializable(patchOperations, requestOptions, resolveEffectiveItemSerializer(requestOptions)); + } + + public static JsonSerializable serializableBatchPatchOperation( + CosmosPatchOperations patchOperations, + RequestOptions requestOptions, + CosmosItemSerializer effectiveItemSerializer) { + + return cosmosPatchToJsonSerializable(patchOperations, requestOptions, resolveEffectiveItemSerializer(effectiveItemSerializer)); } public static byte[] serializeCosmosPatchToByteArray(CosmosPatchOperations patchOperations, RequestOptions requestOptions) { - JsonSerializable jsonSerializable = cosmosPatchToJsonSerializable(patchOperations, requestOptions); + JsonSerializable jsonSerializable = + cosmosPatchToJsonSerializable(patchOperations, requestOptions, resolveEffectiveItemSerializer(requestOptions)); byte[] serializedBody; try { @@ -32,7 +48,11 @@ public static byte[] serializeCosmosPatchToByteArray(CosmosPatchOperations patch return serializedBody; } - private static JsonSerializable cosmosPatchToJsonSerializable(CosmosPatchOperations patchOperations, RequestOptions requestOptions) { + private static JsonSerializable cosmosPatchToJsonSerializable( + CosmosPatchOperations patchOperations, + RequestOptions requestOptions, + CosmosItemSerializer effectiveItemSerializer) { + JsonSerializable jsonSerializable = new JsonSerializable(); ArrayNode operations = Utils.getSimpleObjectMapper().createArrayNode(); List patchOperationList = ModelBridgeInternal.getPatchOperationsFromCosmosPatch(patchOperations); @@ -63,7 +83,9 @@ private static JsonSerializable cosmosPatchToJsonSerializable(CosmosPatchOperati ); operationJsonSerializable.set( PatchConstants.PropertyNames_Value, - ((PatchOperationCore)patchOperation).getResource() + ((PatchOperationCore)patchOperation).getResource(), + effectiveItemSerializer, + true ); } } else { @@ -92,4 +114,25 @@ private static JsonSerializable cosmosPatchToJsonSerializable(CosmosPatchOperati return jsonSerializable; } + + private static CosmosItemSerializer resolveEffectiveItemSerializer(RequestOptions requestOptions) { + if (requestOptions == null) { + return CosmosItemSerializer.DEFAULT_SERIALIZER; + } + + return resolveEffectiveItemSerializer(requestOptions.getEffectiveItemSerializer()); + } + + private static CosmosItemSerializer resolveEffectiveItemSerializer(CosmosItemSerializer candidateSerializer) { + if (candidateSerializer == null) { + return CosmosItemSerializer.DEFAULT_SERIALIZER; + } + + if (candidateSerializer != CosmosItemSerializer.DEFAULT_SERIALIZER + && cosmosItemSerializerAccessor().canSerialize(candidateSerializer)) { + return candidateSerializer; + } + + return CosmosItemSerializer.DEFAULT_SERIALIZER; + } } From 28aa955ffb634071f11508871a4893b9d44f389a Mon Sep 17 00:00:00 2001 From: pure-zero Date: Wed, 25 Nov 2026 13:12:35 +1300 Subject: [PATCH 2/2] fix: increment operations not accounted for in serialization must be numeric --- .../implementation/patch/PatchUnitTest.java | 48 +++++++++++++++++++ .../implementation/patch/PatchUtil.java | 20 +++++--- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java index 9faf34a9ce580..a5528d7e31632 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/patch/PatchUnitTest.java @@ -93,6 +93,54 @@ public void patchOperationValueUsesEffectiveCustomItemSerializer() { assertThat(operation.get(PatchConstants.PropertyNames_Value).get("rawValue").asText()).isEqualTo("originalValue"); } + @Test(groups = { "unit" }) + public void incrementOperationValueBypassesCustomItemSerializerForLong() { + long incr = new Random().nextLong(); + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().increment(path, incr); + + RequestOptions requestOptions = new RequestOptions(); + requestOptions.setEffectiveItemSerializer(new MarkerCosmosItemSerializer()); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation(cosmosPatchOperations, requestOptions); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).isObject()).isFalse(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).isNumber()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).longValue()).isEqualTo(incr); + } + + @Test(groups = { "unit" }) + public void incrementOperationValueBypassesCustomItemSerializerForDouble() { + double incr = new Random().nextDouble(); + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().increment(path, incr); + + RequestOptions requestOptions = new RequestOptions(); + requestOptions.setEffectiveItemSerializer(new MarkerCosmosItemSerializer()); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation(cosmosPatchOperations, requestOptions); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).isObject()).isFalse(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).isNumber()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).doubleValue()).isEqualTo(incr); + } + + @Test(groups = { "unit" }) + public void incrementOperationValueBypassesExplicitEffectiveItemSerializer() { + long incr = new Random().nextLong(); + CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().increment(path, incr); + + RequestOptions requestOptions = new RequestOptions(); + + JsonSerializable serialized = PatchUtil.serializableBatchPatchOperation( + cosmosPatchOperations, requestOptions, new MarkerCosmosItemSerializer()); + ObjectNode operation = (ObjectNode) serialized.getPropertyBag().get(PatchConstants.OPERATIONS).get(0); + + assertThat(operation.get(PatchConstants.PropertyNames_Value).isObject()).isFalse(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).isNumber()).isTrue(); + assertThat(operation.get(PatchConstants.PropertyNames_Value).longValue()).isEqualTo(incr); + } + @Test(groups = { "unit" }) public void patchOperationValueFallsBackToDefaultSerializerWhenNoCustomSerializerConfigured() { CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create().replace(path, "originalValue"); diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java index 31bb2b562f559..d37d1ffa7752d 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/patch/PatchUtil.java @@ -81,12 +81,20 @@ private static JsonSerializable cosmosPatchToJsonSerializable( PatchConstants.PropertyNames_Path, ((PatchOperationCore)patchOperation).getPath() ); - operationJsonSerializable.set( - PatchConstants.PropertyNames_Value, - ((PatchOperationCore)patchOperation).getResource(), - effectiveItemSerializer, - true - ); + + if (patchOperation.getOperationType() == PatchOperationType.INCREMENT) { + operationJsonSerializable.set( + PatchConstants.PropertyNames_Value, + ((PatchOperationCore)patchOperation).getResource() + ); + } else { + operationJsonSerializable.set( + PatchConstants.PropertyNames_Value, + ((PatchOperationCore)patchOperation).getResource(), + effectiveItemSerializer, + true + ); + } } } else { throw new IllegalArgumentException("Invalid patch operation type");