Skip to content

Commit 0ca294b

Browse files
DATAMONGO-1733 - Hacking: revisit fields after review.
1 parent f8e0879 commit 0ca294b

File tree

2 files changed

+14
-34
lines changed

2 files changed

+14
-34
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
import org.springframework.data.mapping.PersistentPropertyAccessor;
6969
import org.springframework.data.mapping.context.MappingContext;
7070
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
71-
import org.springframework.data.mapping.model.Property;
7271
import org.springframework.data.mongodb.MongoDbFactory;
7372
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
7473
import org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext;
@@ -2325,49 +2324,30 @@ private Document getMappedSortObject(Query query, Class<?> type) {
23252324
}
23262325

23272326
private Document getMappedFieldsObject(Document fields, MongoPersistentEntity<?> entity, Class<?> targetType) {
2328-
2329-
Document mappedFields = queryMapper.getMappedFields(fields, entity);
2330-
return addFieldsForProjection(mappedFields, entity.getType(), targetType);
2327+
return queryMapper.getMappedFields(addFieldsForProjection(fields, entity.getType(), targetType), entity);
23312328
}
23322329

23332330
/**
2334-
* For cases where {@code mappedFields} is {@literal null} or {@literal empty} add fields required for creating the
2335-
* projection (target) type if the {@code targetType} is a {@literal closed projection}.
2331+
* For cases where {@code fields} is {@literal null} or {@literal empty} add fields required for creating the
2332+
* projection (target) type if the {@code targetType} is a {@literal closed interface projection}.
23362333
*
2337-
* @param mappedFields can be {@literal null}.
2334+
* @param fields can be {@literal null}.
23382335
* @param domainType must not be {@literal null}.
23392336
* @param targetType must not be {@literal null}.
23402337
* @return {@link Document} with fields to be included.
23412338
*/
2342-
private Document addFieldsForProjection(Document mappedFields, Class<?> domainType, Class<?> targetType) {
2343-
2344-
if ((mappedFields != null && !mappedFields.isEmpty()) || ClassUtils.isAssignable(domainType, targetType)) {
2345-
return mappedFields;
2346-
}
2347-
2348-
Document fields = new Document();
2349-
2350-
if (!targetType.isInterface()) {
2351-
2352-
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(targetType);
2353-
2354-
for (MongoPersistentProperty property : entity) {
2355-
if (!property.isTransient()) {
2356-
fields.append(property.getFieldName(), 1);
2357-
}
2358-
}
2339+
private Document addFieldsForProjection(Document fields, Class<?> domainType, Class<?> targetType) {
23592340

2341+
if ((fields != null && !fields.isEmpty()) || !targetType.isInterface()
2342+
|| ClassUtils.isAssignable(domainType, targetType)) {
23602343
return fields;
23612344
}
23622345

2363-
ProjectionInformation pi = PROJECTION_FACTORY.getProjectionInformation(targetType);
2364-
if (pi.isClosed()) {
2365-
2366-
for (PropertyDescriptor pd : pi.getInputProperties()) {
2346+
ProjectionInformation projectionInformation = PROJECTION_FACTORY.getProjectionInformation(targetType);
2347+
if (projectionInformation.isClosed()) {
23672348

2368-
MongoPersistentProperty pp = ((MongoMappingContext) mappingContext).createPersistentProperty(Property.of(pd),
2369-
((MongoMappingContext) mappingContext).getPersistentEntity(targetType), MongoSimpleTypes.HOLDER);
2370-
fields.append(pp.getFieldName(), 1);
2349+
for (PropertyDescriptor descriptor : projectionInformation.getInputProperties()) {
2350+
fields.append(descriptor.getName(), 1);
23712351
}
23722352
}
23732353

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,15 +820,15 @@ public void doesNotApplyFieldsWhenInterfaceProjectionIsOpen() {
820820
}
821821

822822
@Test // DATAMONGO-1733
823-
public void appliesFieldsToClosedDtoProjectionWhenQueryDoesNotDefineFields() {
823+
public void doesNotApplyFieldsToDtoProjection() {
824824

825825
template.doFind("star-wars", new Document(), new Document(), Person.class, Jedi.class, null);
826826

827-
verify(findIterable).projection(eq(new Document("firstname", 1)));
827+
verify(findIterable, never()).projection(any());
828828
}
829829

830830
@Test // DATAMONGO-1733
831-
public void doesNotApplyFieldsToClosedDtoProjectionWhenQueryDefinesFields() {
831+
public void doesNotApplyFieldsToDtoProjectionWhenQueryDefinesFields() {
832832

833833
template.doFind("star-wars", new Document(), new Document("bar", 1), Person.class, Jedi.class, null);
834834

0 commit comments

Comments
 (0)