You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: skills/cosmosdb-best-practices/AGENTS.md
+62-8Lines changed: 62 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,7 +55,7 @@ Performance optimization and best practices guide for Azure Cosmos DB applicatio
55
55
- 4.6 [Configure SSL and connection mode for Cosmos DB Emulator](#46-configure-ssl-and-connection-mode-for-cosmos-db-emulator)
56
56
- 4.7 [Use ETags for optimistic concurrency on read-modify-write operations](#47-use-etags-for-optimistic-concurrency-on-read-modify-write-operations)
57
57
- 4.8 [Configure Excluded Regions for Dynamic Failover](#48-configure-excluded-regions-for-dynamic-failover)
58
-
- 4.9 [Enable content response on write operations in Java SDK](#49-enable-content-response-on-write-operations-in-java-sdk)
58
+
- 4.9 [Unwrap CosmosItemResponse and enable content response in Java SDK](#49-unwrap-cosmositemresponse-and-enable-content-response-in-java-sdk)
59
59
- 4.10 [Use dependent @Bean methods for Cosmos DB initialization in Spring Boot](#410-use-dependent-bean-methods-for-cosmos-db-initialization-in-spring-boot)
60
60
- 4.11 [Spring Boot and Java version compatibility for Cosmos DB SDK](#411-spring-boot-and-java-version-compatibility-for-cosmos-db-sdk)
61
61
- 4.12 [Configure local development environment to avoid cloud connection conflicts](#412-configure-local-development-environment-to-avoid-cloud-connection-conflicts)
@@ -3589,11 +3589,64 @@ var outageOptions = new ItemRequestOptions
### 4.9 Enable content response on write operations in Java SDK
3592
+
### 4.9 Unwrap CosmosItemResponse and enable content response in Java SDK
3593
3593
3594
-
**Impact: MEDIUM** (ensures created/updated documents are returned from write operations)
3594
+
**Impact: MEDIUM** (prevents type errors from missing getItem() on reads and null content on writes)
3595
3595
3596
-
## Enable Content Response on Write Operations (Java)
3596
+
## Unwrap CosmosItemResponse with getItem() (Java)
3597
+
3598
+
All Cosmos DB Java SDK point-read and write operations (`readItem`, `createItem`, `upsertItem`, `replaceItem`) return `CosmosItemResponse<T>`, **not**`T` directly. You must call `.getItem()` to extract the entity. Treating the response wrapper as the entity causes compilation errors or incorrect behavior.
3599
+
3600
+
### Always unwrap readItem() with getItem()
3601
+
3602
+
`readItem()` always returns `CosmosItemResponse<T>`. You must call `.getItem()` to get the actual document.
3603
+
3604
+
**Incorrect — treating CosmosItemResponse as the entity:**
3605
+
3606
+
```java
3607
+
// ❌ WRONG: readItem returns CosmosItemResponse<Player>, NOT Player
.map(response -> response.getItem()); // ✅ Unwrap to Player
3640
+
}
3641
+
```
3642
+
3643
+
> **Why this matters:**`CosmosItemResponse<T>` is a wrapper that holds the entity (`getItem()`),
3644
+
> request charge (`getRequestCharge()`), ETag (`getETag()`), headers, and diagnostics.
3645
+
> Assigning the response directly to a variable of type `T` is a compile-time error in
3646
+
> synchronous code and a type-mismatch error in reactive chains. This affects `readItem`,
3647
+
> `createItem`, `upsertItem`, and `replaceItem` — all return `CosmosItemResponse<T>`.
3648
+
3649
+
### Enable Content Response on Write Operations
3597
3650
3598
3651
By default, the Java Cosmos DB SDK does **not** return the document content after create/upsert operations. The response contains only metadata (headers, diagnostics) but the `getItem()` method returns null. You must explicitly enable content response if you need the created document.
3599
3652
@@ -3693,11 +3746,12 @@ for (Order order : ordersToInsert) {
3693
3746
Enabling content response does NOT increase RU cost - the document is already fetched server-side for the write operation. It only affects the response payload size over the network.
3694
3747
3695
3748
**Key Points:**
3696
-
- Java SDK returns null by default for created/upserted items
3697
-
- Enable `contentResponseOnWriteEnabled(true)` to get documents back
3749
+
-`readItem()`, `createItem()`, `upsertItem()`, and `replaceItem()` all return `CosmosItemResponse<T>` — always call `.getItem()` to get `T`
3750
+
- In reactive/async code, use `.map(response -> response.getItem())` to unwrap the entity from the `Mono`
3751
+
- Java SDK returns null from `getItem()` by default for created/upserted items — enable `contentResponseOnWriteEnabled(true)` to get documents back after writes
3698
3752
- Can be set at client level (all operations) or per-request
3699
-
- Spring Data Cosmos handles this automatically
3700
-
- Disable for high-throughput scenarios where response content is not needed
3753
+
- Spring Data Cosmos handles both unwrapping and content response automatically
3754
+
- Disable content response for high-throughput scenarios where response content is not needed
3701
3755
3702
3756
Reference: [Azure Cosmos DB Java SDK best practices](https://learn.microsoft.com/azure/cosmos-db/nosql/best-practice-java)
## Enable Content Response on Write Operations (Java)
8
+
## Unwrap CosmosItemResponse with getItem() (Java)
9
+
10
+
All Cosmos DB Java SDK point-read and write operations (`readItem`, `createItem`, `upsertItem`, `replaceItem`) return `CosmosItemResponse<T>`, **not**`T` directly. You must call `.getItem()` to extract the entity. Treating the response wrapper as the entity causes compilation errors or incorrect behavior.
11
+
12
+
### Always unwrap readItem() with getItem()
13
+
14
+
`readItem()` always returns `CosmosItemResponse<T>`. You must call `.getItem()` to get the actual document.
15
+
16
+
**Incorrect — treating CosmosItemResponse as the entity:**
17
+
18
+
```java
19
+
// ❌ WRONG: readItem returns CosmosItemResponse<Player>, NOT Player
.map(response -> response.getItem()); // ✅ Unwrap to Player
52
+
}
53
+
```
54
+
55
+
> **Why this matters:**`CosmosItemResponse<T>` is a wrapper that holds the entity (`getItem()`),
56
+
> request charge (`getRequestCharge()`), ETag (`getETag()`), headers, and diagnostics.
57
+
> Assigning the response directly to a variable of type `T` is a compile-time error in
58
+
> synchronous code and a type-mismatch error in reactive chains. This affects `readItem`,
59
+
> `createItem`, `upsertItem`, and `replaceItem` — all return `CosmosItemResponse<T>`.
60
+
61
+
### Enable Content Response on Write Operations
9
62
10
63
By default, the Java Cosmos DB SDK does **not** return the document content after create/upsert operations. The response contains only metadata (headers, diagnostics) but the `getItem()` method returns null. You must explicitly enable content response if you need the created document.
11
64
@@ -105,10 +158,11 @@ for (Order order : ordersToInsert) {
105
158
Enabling content response does NOT increase RU cost - the document is already fetched server-side for the write operation. It only affects the response payload size over the network.
106
159
107
160
**Key Points:**
108
-
- Java SDK returns null by default for created/upserted items
109
-
- Enable `contentResponseOnWriteEnabled(true)` to get documents back
161
+
-`readItem()`, `createItem()`, `upsertItem()`, and `replaceItem()` all return `CosmosItemResponse<T>` — always call `.getItem()` to get `T`
162
+
- In reactive/async code, use `.map(response -> response.getItem())` to unwrap the entity from the `Mono`
163
+
- Java SDK returns null from `getItem()` by default for created/upserted items — enable `contentResponseOnWriteEnabled(true)` to get documents back after writes
110
164
- Can be set at client level (all operations) or per-request
111
-
- Spring Data Cosmos handles this automatically
112
-
- Disable for high-throughput scenarios where response content is not needed
165
+
- Spring Data Cosmos handles both unwrapping and content response automatically
166
+
- Disable content response for high-throughput scenarios where response content is not needed
113
167
114
168
Reference: [Azure Cosmos DB Java SDK best practices](https://learn.microsoft.com/azure/cosmos-db/nosql/best-practice-java)
0 commit comments