DATAMONGO-2437 - Fix complex id handling when reading aggregation results.
Removed Unwrapping complex id values. Template.aggregate now behaves like aggregateStream or its reactive counterpart. Original pull request: #821.
This commit is contained in:
committed by
Mark Paluch
parent
7b1a96f4a9
commit
d824f3b8b2
@@ -2162,7 +2162,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String collectionName, Class<O> outputType,
|
||||
AggregationOperationContext context) {
|
||||
|
||||
DocumentCallback<O> callback = new UnwrapAndReadDocumentCallback<>(mongoConverter, outputType, collectionName);
|
||||
ReadDocumentCallback<O> callback = new ReadDocumentCallback<>(mongoConverter, outputType, collectionName);
|
||||
|
||||
AggregationOptions options = aggregation.getOptions();
|
||||
AggregationUtil aggregationUtil = new AggregationUtil(queryMapper, mappingContext);
|
||||
@@ -3253,39 +3253,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
}
|
||||
}
|
||||
|
||||
class UnwrapAndReadDocumentCallback<T> extends ReadDocumentCallback<T> {
|
||||
|
||||
public UnwrapAndReadDocumentCallback(EntityReader<? super T, Bson> reader, Class<T> type, String collectionName) {
|
||||
super(reader, type, collectionName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T doWith(@Nullable Document object) {
|
||||
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object idField = object.get(Fields.UNDERSCORE_ID);
|
||||
|
||||
if (!(idField instanceof Document)) {
|
||||
return super.doWith(object);
|
||||
}
|
||||
|
||||
Document toMap = new Document();
|
||||
Document nested = (Document) idField;
|
||||
toMap.putAll(nested);
|
||||
|
||||
for (String key : object.keySet()) {
|
||||
if (!Fields.UNDERSCORE_ID.equals(key)) {
|
||||
toMap.put(key, object.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
return super.doWith(toMap);
|
||||
}
|
||||
}
|
||||
|
||||
class QueryCursorPreparer implements CursorPreparer {
|
||||
|
||||
private final Query query;
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate.UnwrapAndReadDocumentCallback;
|
||||
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
|
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link UnwrapAndReadDocumentCallback}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class UnwrapAndReadDocumentCallbackUnitTests {
|
||||
|
||||
@Mock MongoDbFactory factory;
|
||||
@Mock MongoExceptionTranslator exceptionTranslatorMock;
|
||||
|
||||
UnwrapAndReadDocumentCallback<Target> callback;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
when(factory.getExceptionTranslator()).thenReturn(exceptionTranslatorMock);
|
||||
|
||||
MongoTemplate template = new MongoTemplate(factory);
|
||||
MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory),
|
||||
new MongoMappingContext());
|
||||
|
||||
this.callback = template.new UnwrapAndReadDocumentCallback(converter, Target.class, "collection-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesFirstLevelValues() {
|
||||
|
||||
Target target = callback.doWith(new Document("foo", "bar"));
|
||||
|
||||
assertThat(target.id).isNull();
|
||||
assertThat(target.foo).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unwrapsUnderscoreIdIfDocument() {
|
||||
|
||||
Target target = callback.doWith(new Document("_id", new Document("foo", "bar")));
|
||||
|
||||
assertThat(target.id).isNull();
|
||||
assertThat(target.foo).isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void firstLevelPropertiesTrumpNestedOnes() {
|
||||
|
||||
Target target = callback.doWith(new Document("_id", new Document("foo", "bar")).append("foo", "foobar"));
|
||||
|
||||
assertThat(target.id).isNull();
|
||||
assertThat(target.foo).isEqualTo("foobar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keepsUnderscoreIdIfScalarValue() {
|
||||
|
||||
Target target = callback.doWith(new Document("_id", "bar").append("foo", "foo"));
|
||||
|
||||
assertThat(target.id).isEqualTo("bar");
|
||||
assertThat(target.foo).isEqualTo("foo");
|
||||
}
|
||||
|
||||
static class Target {
|
||||
|
||||
String id;
|
||||
String foo;
|
||||
}
|
||||
}
|
||||
@@ -1823,7 +1823,7 @@ public class AggregationTests {
|
||||
assertThat((Double) bound100.get("sum")).isCloseTo(3672.9, Offset.offset(0.1D));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1552
|
||||
@Test // DATAMONGO-1552, DATAMONGO-2437
|
||||
@MongoVersion(asOf = "3.4")
|
||||
public void bucketAutoShouldCollectDocumentsIntoABucket() {
|
||||
|
||||
@@ -1844,14 +1844,14 @@ public class AggregationTests {
|
||||
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, Document.class);
|
||||
assertThat(result.getMappedResults().size()).isEqualTo(3);
|
||||
|
||||
// { "min" : 680.0 , "max" : 820.0 , "count" : 1 , "titles" : [ "Dancer"] , "sum" : 760.4000000000001}
|
||||
// { "_id" : { "min" : 680.0 , "max" : 820.0 }, "count" : 1 , "titles" : [ "Dancer"] , "sum" : 760.4000000000001}
|
||||
Document bound0 = result.getMappedResults().get(0);
|
||||
assertThat(bound0).containsEntry("count", 1).containsEntry("titles.[0]", "Dancer").containsEntry("min", 680.0)
|
||||
.containsKey("max");
|
||||
assertThat(bound0).containsEntry("count", 1).containsEntry("titles.[0]", "Dancer").containsEntry("_id.min", 680.0)
|
||||
.containsKey("_id.max");
|
||||
|
||||
// { "min" : 820.0 , "max" : 1800.0 , "count" : 1 , "titles" : [ "The Great Wave off Kanagawa"] , "sum" : 1673.0}
|
||||
// { "_id" : { "min" : 820.0 , "max" : 1800.0 }, "count" : 1 , "titles" : [ "The Great Wave off Kanagawa"] , "sum" : 1673.0}
|
||||
Document bound1 = result.getMappedResults().get(1);
|
||||
assertThat(bound1).containsEntry("count", 1).containsEntry("min", 820.0);
|
||||
assertThat(bound1).containsEntry("count", 1).containsEntry("_id.min", 820.0);
|
||||
assertThat((List<String>) bound1.get("titles")).contains("The Great Wave off Kanagawa");
|
||||
assertThat((Double) bound1.get("sum")).isCloseTo(1673.0, Offset.offset(0.1D));
|
||||
}
|
||||
@@ -1930,6 +1930,21 @@ public class AggregationTests {
|
||||
assertThat(groupResults.getMappedResults().size()).isEqualTo(4);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2437
|
||||
public void shouldReadComplexIdValueCorrectly() {
|
||||
|
||||
WithComplexId source = new WithComplexId();
|
||||
source.id = new ComplexId();
|
||||
source.id.p1 = "v1";
|
||||
source.id.p2 = "v2";
|
||||
|
||||
mongoTemplate.save(source);
|
||||
|
||||
AggregationResults<WithComplexId> result = mongoTemplate.aggregate(newAggregation(project("id")),
|
||||
WithComplexId.class, WithComplexId.class);
|
||||
assertThat(result.getMappedResults()).containsOnly(source);
|
||||
}
|
||||
|
||||
private void createUsersWithReferencedPersons() {
|
||||
|
||||
mongoTemplate.dropCollection(User.class);
|
||||
@@ -2231,4 +2246,15 @@ public class AggregationTests {
|
||||
Integer year;
|
||||
double price;
|
||||
}
|
||||
|
||||
@lombok.Data
|
||||
static class WithComplexId {
|
||||
@Id ComplexId id;
|
||||
}
|
||||
|
||||
@lombok.Data
|
||||
static class ComplexId {
|
||||
String p1;
|
||||
String p2;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user