Azure / Azure/azure-sdk-for-java

[BUG] Azure Cosmos Java sdk not validating item ids

Open
#47,593 2 comments 0 reactions 1 assignee Assigned to @kushagraThapar View on GitHub
bug Client Cosmos 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**
Azure Java SDK for Cosmos lacks proper validation when inserting rows into a container.
As we have verified in [docs](https://learn.microsoft.com/en-us/azure/cosmos-db/concepts-limits) for allowed characters in an id, plus trying this hands on in the azure console, whenever we try to create one row with id including e.g. the '/' character we get this error: `Illegal characters ['/', '\', '#'] cannot be used in Resource ID.`
However, the java client allows these characters. If you try to process/delete them it's also not possible with the java client as it reaches a not found error. So this can lead to corrupted data inside a cosmos container(as we realized) instead of failing fast and not allowing those at all.

***Exception or Stack Trace***
Adding only the stacktrace for the deletion, as insertion is always successful.
```
Exception in thread "main" java.lang.IllegalArgumentException: Entity with the specified id does not exist in the system. More info: https://aka.ms/cosmosdb-tsg-not-found-java, - Path: /dbs/cdp-test-sessions-db/colls/active_sessions2/docs/clientKeyTest__386fd1c9-1bb0-403d-be24-10d4c0fae5db__kkia/en/
at com.azure.cosmos.implementation.RxDocumentServiceRequest.(RxDocumentServiceRequest.java:254)
at com.azure.cosmos.implementation.RxDocumentServiceRequest.(RxDocumentServiceRequest.java:274)
at com.azure.cosmos.implementation.RxDocumentServiceRequest.(RxDocumentServiceRequest.java:290)
at com.azure.cosmos.implementation.RxDocumentServiceRequest.(RxDocumentServiceRequest.java:319)
at com.azure.cosmos.implementation.RxDocumentServiceRequest.create(RxDocumentServiceRequest.java:559)
at com.azure.cosmos.implementation.RxDocumentClientImpl.deleteDocumentInternal(RxDocumentClientImpl.java:3733)
at com.azure.cosmos.implementation.RxDocumentClientImpl.lambda$deleteDocumentCore$93(RxDocumentClientImpl.java:3704)
at com.azure.cosmos.implementation.ObservableHelper.lambda$inlineIfPossibleAsObs$1(ObservableHelper.java:44)
at com.azure.cosmos.implementation.BackoffRetryUtility.lambda$executeRetry$0(BackoffRetryUtility.java:36)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:45)
at reactor.core.publisher.FluxRetryWhen.subscribe(FluxRetryWhen.java:81)
at reactor.core.publisher.MonoRetryWhen.subscribeOrReturn(MonoRetryWhen.java:46)
at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:63)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:165)
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2570)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.request(MonoFlatMap.java:194)
at reactor.core.publisher.FluxDoOnEach$DoOnEachSubscriber.request(FluxDoOnEach.java:108)
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.request(FluxContextWrite.java:136)
at reactor.core.publisher.BlockingSingleSubscriber.onSubscribe(BlockingSingleSubscriber.java:54)
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onSubscribe(FluxContextWrite.java:101)
at reactor.core.publisher.FluxDoOnEach$DoOnEachSubscriber.onSubscribe(FluxDoOnEach.java:121)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onSubscribe(MonoFlatMap.java:117)
at com.azure.cosmos.implementation.DiagnosticsProvider.subscribe(DiagnosticsProvider.java:1064)
at com.azure.cosmos.implementation.DiagnosticsProvider.access$300(DiagnosticsProvider.java:76)
at com.azure.cosmos.implementation.DiagnosticsProvider$PropagatingMono.subscribe(DiagnosticsProvider.java:1090)
at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:76)
at reactor.core.publisher.MonoDeferContextual.subscribe(MonoDeferContextual.java:55)
at reactor.core.publisher.Mono.subscribe(Mono.java:4576)
at reactor.core.publisher.Mono.block(Mono.java:1778)
at com.sitecore.cosmos.integration.ChangeFeedServiceSetup2Utils.main(ChangeFeedServiceSetup2Utils.java:114)
Suppressed: java.lang.Exception: #block terminated with an error
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:104)
at reactor.core.publisher.Mono.block(Mono.java:1779)
... 1 more
```

**To Reproduce**
Insert with the java sdk an item in a container with an id like this:
`clientKeyTest__386fd1c9-1bb0-403d-be24-10d4c0fae5db__kkia/en`
You can see it will appear in the container successfully. Then you cannot view/delete this with the java client.
You cannot delete it through the azure console, you cannot delete it through azure console.

***Code Snippet***
```
ObjectNode testDocument = objectMapper.createObjectNode();
testDocument.put("id", "clientKeyTest__386fd1c9-1bb0-403d-be24-10d4c0fae5db__kkia/en");
testDocument.put("shardId", 1);

CosmosAsyncClient cosmosClient = new CosmosClientBuilder()
.endpoint(endpoint)
.key(key)
.consistencyLevel(ConsistencyLevel.SESSION)
.contentResponseOnWriteEnabled(false)
.gatewayMode(new GatewayConnectionConfig()
.setMaxConnectionPoolSize(1000)
.setIdleConnectionTimeout(Duration.ofSeconds(60)))
.buildAsyncClient();
CosmosAsyncDatabase sharedDb = cosmosClient.getDatabase(sharedDatabase);
CosmosAsyncContainer container = sharedDb.getContainer(CONTAINER_NAME);

container.createItem(testDocument, new PartitionKey(doc.get("shardId").asInt()),
new CosmosItemRequestOptions()));
```

**Expected behavior**
If Azure Cosmos is disallowing illegal characters in ids the java client should return a validation error first place instead of allowing you to add such in the container and then having trouble processing and removing them.

**Screenshots**

Image

**Setup (please complete the following information):**
- OS: Azure Linux 3.0
AZURELINUX_BUILD_NUMBER=3.0.20251206
DISTRIB_ID="azurelinux"

- IDE: IntelliJ
- Library/Libraries: implementation("com.azure:azure-cosmos:4.76.0")
- Java version: openjdk version "25.0.1" 2025-10-21 LTS
OpenJDK Runtime Environment Microsoft-12574220 (build 25.0.1+8-LTS)
- App Server/Environment: [e.g. Tomcat, WildFly, Azure Function, Apache Spark, Databricks, IDE plugin or anything special]
- Frameworks: Micronaut

**Additional context**
Add any other context about the problem here.

**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.