Skip to content

Commit 65b058f

Browse files
christophstroblmp911de
authored andcommitted
Fix SpEL evaluation in document reference lookup.
Closes #3842 Original pull request: #3844.
1 parent 4a5789d commit 65b058f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.data.mongodb.util.json.ParameterBindingContext;
4444
import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec;
4545
import org.springframework.data.mongodb.util.json.ValueProvider;
46+
import org.springframework.data.mongodb.util.spel.ExpressionUtils;
4647
import org.springframework.data.util.Streamable;
4748
import org.springframework.expression.EvaluationContext;
4849
import org.springframework.lang.Nullable;
@@ -197,6 +198,10 @@ private <T> T parseValueOrGet(String value, ParameterBindingContext bindingConte
197198
return (T) codec.decode(value, bindingContext);
198199
}
199200

201+
if(!value.startsWith("#") && ExpressionUtils.detectExpression(value) == null) {
202+
return (T) value;
203+
}
204+
200205
T evaluated = (T) bindingContext.evaluateExpression(value);
201206
return evaluated != null ? evaluated : defaultValue.get();
202207
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
24+
import org.assertj.core.api.Assertions;
25+
import org.bson.Document;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.mockito.Mock;
30+
import org.mockito.junit.jupiter.MockitoExtension;
31+
import org.springframework.data.mapping.context.MappingContext;
32+
import org.springframework.data.mapping.model.SpELContext;
33+
import org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery;
34+
import org.springframework.data.mongodb.core.convert.ReferenceLookupDelegate.LookupFunction;
35+
import org.springframework.data.mongodb.core.convert.ReferenceResolver.MongoEntityReader;
36+
import org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection;
37+
import org.springframework.data.mongodb.core.mapping.DocumentReference;
38+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
39+
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
40+
import org.springframework.expression.EvaluationContext;
41+
import org.springframework.expression.spel.standard.SpelExpressionParser;
42+
43+
/**
44+
* @author Christoph Strobl
45+
*/
46+
@ExtendWith(MockitoExtension.class)
47+
class ReferenceLookupDelegateUnitTests {
48+
49+
@Mock MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
50+
@Mock SpELContext spELContext;
51+
@Mock EvaluationContext evaluationContext;
52+
@Mock MongoEntityReader entityReader;
53+
54+
private ReferenceLookupDelegate lookupDelegate;
55+
56+
@BeforeEach
57+
void beforeEach() {
58+
59+
lookupDelegate = new ReferenceLookupDelegate(mappingContext, spELContext);
60+
when(spELContext.getParser()).thenReturn(new SpelExpressionParser());
61+
}
62+
63+
@Test // GH-3842
64+
void shouldComputePlainStringTargetCollection() {
65+
66+
DocumentReference documentReference = mock(DocumentReference.class);
67+
MongoPersistentEntity entity = mock(MongoPersistentEntity.class);
68+
MongoPersistentProperty property = mock(MongoPersistentProperty.class);
69+
70+
doReturn(entity).when(mappingContext).getRequiredPersistentEntity((Class) any());
71+
72+
when(property.isDocumentReference()).thenReturn(true);
73+
when(property.getDocumentReference()).thenReturn(documentReference);
74+
when(documentReference.collection()).thenReturn("collection1");
75+
76+
lookupDelegate.readReference(property, Arrays.asList("one"), new LookupFunction() {
77+
@Override
78+
public Iterable<Document> apply(DocumentReferenceQuery referenceQuery, ReferenceCollection referenceCollection) {
79+
80+
assertThat(referenceCollection.getCollection()).isEqualTo("collection1");
81+
return Collections.emptyList();
82+
}
83+
}, entityReader);
84+
}
85+
}

0 commit comments

Comments
 (0)