Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mapping cosmos converter to handle value nodes #22073

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -111,6 +113,50 @@ public void testAnnotatedQueryWithSort() {
assertAddressOrder(resultsDesc, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION1);
}

@Test
public void testAnnotatedQueryWithValueAsPage() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
addressRepository.saveAll(addresses);

final PageRequest cosmosPageRequest = CosmosPageRequest.of(0, 10);
final Page<String> postalCodes = addressRepository.annotatedFindPostalCodeValuesByCity(TestConstants.CITY,
cosmosPageRequest);

assertAddressPostalCodes(postalCodes.getContent(), addresses);
}

@Test
public void testAnnotatedQueryWithValueAsList() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
addressRepository.saveAll(addresses);

final List<String> postalCodes = addressRepository.annotatedFindPostalCodeValuesByCity(TestConstants.CITY);

assertAddressPostalCodes(postalCodes, addresses);
}

@Test
public void testAnnotatedQueryWithJsonNodeAsPage() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
addressRepository.saveAll(addresses);

final PageRequest cosmosPageRequest = CosmosPageRequest.of(0, 10);
final Page<JsonNode> postalCodes = addressRepository.annotatedFindPostalCodesByCity(TestConstants.CITY,
cosmosPageRequest);
final List<String> actualPostalCodes = postalCodes.getContent()
.stream()
.map(jsonNode -> jsonNode.get("postalCode").asText())
.collect(Collectors.toList());
assertAddressPostalCodes(actualPostalCodes, addresses);
}

private void assertAddressPostalCodes(List<String> postalCodes, List<Address> expectedResults) {
List<String> expectedPostalCodes = expectedResults.stream()
.map(Address::getPostalCode)
.collect(Collectors.toList());
assertThat(postalCodes).isEqualTo(expectedPostalCodes);
}

private void assertAddressOrder(List<Address> actualResults, Address ... expectedResults) {
assertThat(actualResults.size()).isEqualTo(expectedResults.length);
for (int i = 0; i < expectedResults.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -245,4 +248,16 @@ public void testAnnotatedQueries() {
List<ObjectNode> groupByContacts = repository.selectGroupBy();
Assert.assertEquals(3, groupByContacts.size());
}

@Test
public void testAnnotatedQueriesDistinctIntValue() {
List<Integer> valueContacts = repository.findDistinctIntValueValues();
assertThat(valueContacts).isEqualTo(Arrays.asList(INT_VALUE_1, INT_VALUE_2, INT_VALUE_3));
}

@Test
public void testAnnotatedQueriesDistinctStatus() {
List<Boolean> statusContacts = repository.findDistinctStatusValues();
assertThat(statusContacts).isEqualTo(Arrays.asList(Boolean.TRUE, Boolean.FALSE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,4 +37,12 @@ public interface AddressRepository extends CosmosRepository<Address, String> {
@Query("select * from a where a.city = @city")
List<Address> annotatedFindByCity(@Param("city") String city, Sort pageable);

@Query("select DISTINCT value a.postalCode from a where a.city = @city")
Page<String> annotatedFindPostalCodeValuesByCity(@Param("city") String city, Pageable pageable);

@Query("select DISTINCT a.postalCode from a where a.city = @city")
Page<JsonNode> annotatedFindPostalCodesByCity(@Param("city") String city, Pageable pageable);

@Query("select DISTINCT value a.postalCode from a where a.city = @city")
List<String> annotatedFindPostalCodeValuesByCity(@Param("city") String city);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public interface ContactRepository extends CosmosRepository<Contact, String> {
@Query(value = "SELECT count(c.id) as id_count, c.intValue FROM c group by c.intValue")
List<ObjectNode> selectGroupBy();

@Query(value = "Select DISTINCT value c.intValue from c")
List<Integer> findDistinctIntValueValues();

@Query(value = "Select DISTINCT value c.active from c")
List<Boolean> findDistinctStatusValues();

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public MappingCosmosConverter(
public <R> R read(Class<R> type, JsonNode jsonNode) {

final CosmosPersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
Assert.notNull(entity, "Entity is null.");

return readInternal(entity, type, jsonNode);
}
Expand All @@ -74,6 +73,10 @@ public void write(Object source, JsonNode sink) {

private <R> R readInternal(final CosmosPersistentEntity<?> entity, Class<R> type,
final JsonNode jsonNode) {
if (jsonNode.isValueNode()) {
return readJsonNodeAsValue(jsonNode, type);
}
Assert.notNull(entity, "Entity is null.");
final ObjectNode objectNode = jsonNode.deepCopy();
try {
final CosmosPersistentProperty idProperty = entity.getIdProperty();
Expand All @@ -96,6 +99,27 @@ private <R> R readInternal(final CosmosPersistentEntity<?> entity, Class<R> type
}
}

private <R> R readJsonNodeAsValue(JsonNode jsonNode, Class<R> type) {
kushagraThapar marked this conversation as resolved.
Show resolved Hide resolved
switch (jsonNode.getNodeType()) {
case BOOLEAN:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we special case ARRAY here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FabianMeiswinkel - updated the logic to convert value nodes, objectMapper should automatically take care of that. Will test it out as well to verify.

return objectMapper.convertValue(jsonNode.asBoolean(), type);
case NUMBER:
if (jsonNode.isInt()) {
return objectMapper.convertValue(jsonNode.asInt(), type);
} else if (jsonNode.isLong()) {
return objectMapper.convertValue(jsonNode.asLong(), type);
} else if (jsonNode.isDouble()) {
return objectMapper.convertValue(jsonNode.asDouble(), type);
} else {
return objectMapper.convertValue(jsonNode, type);
}
case STRING:
return objectMapper.convertValue(jsonNode.asText(), type);
default:
return objectMapper.convertValue(jsonNode, type);
}
}

/**
* To write source entity as a cosmos item
*
Expand Down