microsoft / spring-data-cosmosdb

Access data with Azure Cosmos DB
MIT License
93 stars 68 forks source link

Sorting throws exception when pageSize is less than total items in repository #423

Closed marcokleijer closed 4 years ago

marcokleijer commented 4 years ago

When I have a Cosmos DB configured with SQL Api, and use spring-data-cosmosdb to write and read from this database, and I try to paginate and sort with a pageSize less than the total items available in the database, I get the following exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class ch.qos.logback.core.spi.LogbackLock and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.azure.data.cosmos.internal.query.QueryItem[0]->com.azure.data.cosmos.internal.query.QueryItem["logger"]->ch.qos.logback.classic.Logger["loggerContext"]->ch.qos.logback.classic.LoggerContext["configurationLock"])

which boils down to the following code

at com.azure.data.cosmos.JsonSerializable.set(JsonSerializable.java:155) ~[azure-cosmos-3.1.0.jar:na]
at com.azure.data.cosmos.BridgeInternal.setProperty(BridgeInternal.java:303) ~[azure-cosmos-3.1.0.jar:na]
at com.azure.data.cosmos.internal.query.OrderByContinuationToken.setOrderByItems(OrderByContinuationToken.java:123) ~[azure-cosmos-3.1.0.jar:na]
at com.azure.data.cosmos.internal.query.OrderByContinuationToken.<init>(OrderByContinuationToken.java:47) ~[azure-cosmos-3.1.0.jar:na]
at com.azure.data.cosmos.internal.query.OrderByDocumentQueryExecutionContext.getContinuationToken(OrderByDocumentQueryExecutionContext.java:569) ~[azure-cosmos-3.1.0.jar:na]
at com.azure.data.cosmos.internal.query.OrderByDocumentQueryExecutionContext$$Lambda$929/2004043526.apply(Unknown Source) ~[na:na]
at com.azure.data.cosmos.internal.query.OrderByDocumentQueryExecutionContext$ItemToPageTransformer.lambda$apply$4(OrderByDocumentQueryExecutionContext.java:484) ~[azure-cosmos-3.1.0.jar:na]
at com.azure.data.cosmos.internal.query.OrderByDocumentQueryExecutionContext$ItemToPageTransformer$$Lambda$936/836770123.apply(Unknown Source) ~[na:na]

Use attached project to reproduce the issue. paging-sorting-issue.zip

marcokleijer commented 4 years ago

For now I have a workaround by overriding the serialization configuration of QueryItem with: Utils.getSimpleObjectMapper().addMixIn(QueryItem.class, QueryItemMixIn.class); and:

interface QueryItemMixIn {
  @JsonIgnore
  abstract Logger getLogger();
}

Problem lies in Jackson trying to serialize the Logger exposed by com.azure.data.cosmos.JsonSerializable#getLogger() which is inherited by QueryItem.

kushagraThapar commented 4 years ago

@marcokleijer This is a bug on Cosmos SDK side. I have tried to fix it here: https://github.com/Azure/azure-sdk-for-java/pull/5951

If the PR gets in, this should be fixed in 2.2.0

For the time being, I have also included this fix in this PR: https://github.com/microsoft/spring-data-cosmosdb/pull/445

kushagraThapar commented 4 years ago

This has been released in spring-data-cosmosdb v2.2.0