diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java
index a343c5e2f8eb..2c62255b0acb 100644
--- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java
+++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/AnnotatedQueryIT.java
@@ -11,6 +11,7 @@
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
import com.azure.spring.data.cosmos.repository.repository.AddressRepository;
import com.azure.spring.data.cosmos.repository.repository.AuditableRepository;
+import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
@@ -25,6 +26,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
+import java.util.stream.Collectors;
import static com.azure.spring.data.cosmos.common.PageTestUtils.validateLastPage;
import static com.azure.spring.data.cosmos.common.PageTestUtils.validateNonLastPage;
@@ -111,6 +113,50 @@ public void testAnnotatedQueryWithSort() {
assertAddressOrder(resultsDesc, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION1);
}
+ @Test
+ public void testAnnotatedQueryWithValueAsPage() {
+ final List
addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
+ addressRepository.saveAll(addresses);
+
+ final PageRequest cosmosPageRequest = CosmosPageRequest.of(0, 10);
+ final Page postalCodes = addressRepository.annotatedFindPostalCodeValuesByCity(TestConstants.CITY,
+ cosmosPageRequest);
+
+ assertAddressPostalCodes(postalCodes.getContent(), addresses);
+ }
+
+ @Test
+ public void testAnnotatedQueryWithValueAsList() {
+ final List addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
+ addressRepository.saveAll(addresses);
+
+ final List postalCodes = addressRepository.annotatedFindPostalCodeValuesByCity(TestConstants.CITY);
+
+ assertAddressPostalCodes(postalCodes, addresses);
+ }
+
+ @Test
+ public void testAnnotatedQueryWithJsonNodeAsPage() {
+ final List addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
+ addressRepository.saveAll(addresses);
+
+ final PageRequest cosmosPageRequest = CosmosPageRequest.of(0, 10);
+ final Page postalCodes = addressRepository.annotatedFindPostalCodesByCity(TestConstants.CITY,
+ cosmosPageRequest);
+ final List actualPostalCodes = postalCodes.getContent()
+ .stream()
+ .map(jsonNode -> jsonNode.get("postalCode").asText())
+ .collect(Collectors.toList());
+ assertAddressPostalCodes(actualPostalCodes, addresses);
+ }
+
+ private void assertAddressPostalCodes(List postalCodes, List expectedResults) {
+ List expectedPostalCodes = expectedResults.stream()
+ .map(Address::getPostalCode)
+ .collect(Collectors.toList());
+ assertThat(postalCodes).isEqualTo(expectedPostalCodes);
+ }
+
private void assertAddressOrder(List actualResults, Address ... expectedResults) {
assertThat(actualResults.size()).isEqualTo(expectedResults.length);
for (int i = 0; i < expectedResults.length; i++) {
diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java
index 67fd40e5234b..bef63c503ffb 100644
--- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java
+++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java
@@ -34,11 +34,14 @@
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class ContactRepositoryIT {
- private static final Contact TEST_CONTACT1 = new Contact("testId", "faketitle", 25, true);
- private static final Contact TEST_CONTACT2 = new Contact("testId2", "faketitle2", 32, false);
- private static final Contact TEST_CONTACT3 = new Contact("testId3", "faketitle3", 25, false);
- private static final Contact TEST_CONTACT4 = new Contact("testId4", "faketitle4", 43, true);
- private static final Contact TEST_CONTACT5 = new Contact("testId5", "faketitle3", 43, true);
+ private static final Integer INT_VALUE_1 = 25;
+ private static final Integer INT_VALUE_2 = 32;
+ private static final Integer INT_VALUE_3 = 43;
+ private static final Contact TEST_CONTACT1 = new Contact("testId", "faketitle", INT_VALUE_1, true);
+ private static final Contact TEST_CONTACT2 = new Contact("testId2", "faketitle2", INT_VALUE_2, false);
+ private static final Contact TEST_CONTACT3 = new Contact("testId3", "faketitle3", INT_VALUE_1, false);
+ private static final Contact TEST_CONTACT4 = new Contact("testId4", "faketitle4", INT_VALUE_3, true);
+ private static final Contact TEST_CONTACT5 = new Contact("testId5", "faketitle3", INT_VALUE_3, true);
@ClassRule
public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager();
@@ -245,4 +248,16 @@ public void testAnnotatedQueries() {
List groupByContacts = repository.selectGroupBy();
Assert.assertEquals(3, groupByContacts.size());
}
+
+ @Test
+ public void testAnnotatedQueriesDistinctIntValue() {
+ List valueContacts = repository.findDistinctIntValueValues();
+ assertThat(valueContacts).isEqualTo(Arrays.asList(INT_VALUE_1, INT_VALUE_2, INT_VALUE_3));
+ }
+
+ @Test
+ public void testAnnotatedQueriesDistinctStatus() {
+ List statusContacts = repository.findDistinctStatusValues();
+ assertThat(statusContacts).isEqualTo(Arrays.asList(Boolean.TRUE, Boolean.FALSE));
+ }
}
diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/AddressRepository.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/AddressRepository.java
index 6e5824a70de2..f4054e5d8198 100644
--- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/AddressRepository.java
+++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/AddressRepository.java
@@ -5,6 +5,7 @@
import com.azure.spring.data.cosmos.domain.Address;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
+import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@@ -36,4 +37,12 @@ public interface AddressRepository extends CosmosRepository {
@Query("select * from a where a.city = @city")
List annotatedFindByCity(@Param("city") String city, Sort pageable);
+ @Query("select DISTINCT value a.postalCode from a where a.city = @city")
+ Page annotatedFindPostalCodeValuesByCity(@Param("city") String city, Pageable pageable);
+
+ @Query("select DISTINCT a.postalCode from a where a.city = @city")
+ Page annotatedFindPostalCodesByCity(@Param("city") String city, Pageable pageable);
+
+ @Query("select DISTINCT value a.postalCode from a where a.city = @city")
+ List annotatedFindPostalCodeValuesByCity(@Param("city") String city);
}
diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ContactRepository.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ContactRepository.java
index 1e4bfda3cd50..fb8a8afaa7c8 100644
--- a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ContactRepository.java
+++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ContactRepository.java
@@ -44,4 +44,10 @@ public interface ContactRepository extends CosmosRepository {
@Query(value = "SELECT count(c.id) as id_count, c.intValue FROM c group by c.intValue")
List selectGroupBy();
+ @Query(value = "Select DISTINCT value c.intValue from c")
+ List findDistinctIntValueValues();
+
+ @Query(value = "Select DISTINCT value c.active from c")
+ List findDistinctStatusValues();
+
}
diff --git a/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/convert/MappingCosmosConverter.java b/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/convert/MappingCosmosConverter.java
index 6c8a7f4472ca..556a79f7ea43 100644
--- a/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/convert/MappingCosmosConverter.java
+++ b/sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/convert/MappingCosmosConverter.java
@@ -62,7 +62,6 @@ public MappingCosmosConverter(
public R read(Class type, JsonNode jsonNode) {
final CosmosPersistentEntity> entity = mappingContext.getPersistentEntity(type);
- Assert.notNull(entity, "Entity is null.");
return readInternal(entity, type, jsonNode);
}
@@ -74,8 +73,13 @@ public void write(Object source, JsonNode sink) {
private R readInternal(final CosmosPersistentEntity> entity, Class type,
final JsonNode jsonNode) {
- final ObjectNode objectNode = jsonNode.deepCopy();
try {
+ if (jsonNode.isValueNode()) {
+ return objectMapper.treeToValue(jsonNode, type);
+ }
+
+ Assert.notNull(entity, "Entity is null.");
+ final ObjectNode objectNode = jsonNode.deepCopy();
final CosmosPersistentProperty idProperty = entity.getIdProperty();
final JsonNode idValue = jsonNode.get("id");
if (idProperty != null) {
@@ -90,7 +94,7 @@ private R readInternal(final CosmosPersistentEntity> entity, Class type
return objectMapper.treeToValue(objectNode, type);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Failed to read the source document "
- + objectNode.toPrettyString()
+ + jsonNode.toPrettyString()
+ " to target type "
+ type, e);
}