DATAMONGO-2517 - Fix invalid entity creation for text queries.

Fix a glitch in the MappingMongoConverter that uses the single String argument constructor (since it matches in type and parameter count to the given input string) to falsely instantiate an Entity when it should not.

Original pull request: #857.
This commit is contained in:
Christoph Strobl
2020-04-17 11:15:42 +02:00
committed by Mark Paluch
parent e1df28797a
commit 6604c507dd
2 changed files with 29 additions and 3 deletions

View File

@@ -1283,9 +1283,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
}
if (conversions.isSimpleType(obj.getClass())) {
// Doesn't need conversion
return getPotentiallyConvertedSimpleWrite(obj,
typeInformation != null ? typeInformation.getType() : Object.class);
Class<?> conversionTargetType = Object.class;
if(typeInformation != null && conversions.isSimpleType(typeInformation.getType())) {
conversionTargetType = typeInformation.getType();
}
return getPotentiallyConvertedSimpleWrite(obj, conversionTargetType);
}
if (obj instanceof List) {

View File

@@ -56,6 +56,7 @@ import org.springframework.data.mongodb.core.mapping.TextScore;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.TextQuery;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClientSettings;
@@ -987,6 +988,16 @@ public class QueryMapperUnitTests {
.isEqualTo(new org.bson.Document("level0.$[some_item].arrayCustomName.$[other_item].nes-ted", "value"));
}
@Test // DATAMONGO-2517
public void shouldParseNestedKeywordWithArgumentMatchingTheSourceEntitiesConstructorCorrectly() {
TextQuery source = new TextQuery("test");
org.bson.Document target = mapper.getMappedObject(source.getQueryObject(),
context.getPersistentEntity(WithSingleStringArgConstructor.class));
assertThat(target).isEqualTo(org.bson.Document.parse("{\"$text\" : { \"$search\" : \"test\" }}"));
}
class WithDeepArrayNesting {
List<WithNestedArray> level0;
@@ -1158,4 +1169,16 @@ public class QueryMapperUnitTests {
String id;
@Field(targetType = FieldType.OBJECT_ID) String stringAsOid;
}
@Document
static class WithSingleStringArgConstructor {
String value;
public WithSingleStringArgConstructor() {}
public WithSingleStringArgConstructor(String value) {
this.value = value;
}
}
}