Skip to content

Commit 9e18a54

Browse files
committed
DATAMONGO-1245 - Remove escaping for native regex string matching.
1 parent b77e21b commit 9e18a54

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222

2323
/**
2424
* @author Christoph Strobl
25+
* @author Mark Paluch
2526
* @since 1.8
2627
*/
2728
public enum MongoRegexCreator {
@@ -67,6 +68,10 @@ public String toRegularExpression(String source, Type type) {
6768

6869
private String prepareAndEscapeStringBeforeApplyingLikeRegex(String source, Type type) {
6970

71+
if (ObjectUtils.nullSafeEquals(Type.REGEX, type)) {
72+
return source;
73+
}
74+
7075
if (!ObjectUtils.nullSafeEquals(Type.LIKE, type)) {
7176
return PUNCTATION_PATTERN.matcher(source).find() ? Pattern.quote(source) : source;
7277
}

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

+43-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@
4949

5050
/**
5151
* @author Christoph Strobl
52+
* @author Mark Paluch
5253
*/
5354
@RunWith(MockitoJUnitRunner.class)
5455
public class MongoExampleMapperUnitTests {
@@ -149,7 +150,7 @@ public void exampleShouldBeMappedCorrectlyForFlatTypeWhenFieldNameIsCustomized()
149150
* @see DATAMONGO-1245
150151
*/
151152
@Test
152-
public void exampleShouldBeMappedAsFlatMapWhenGivenNestedElementsWithLenienMatchMode() {
153+
public void exampleShouldBeMappedAsFlatMapWhenGivenNestedElementsWithLenientMatchMode() {
153154

154155
WrapperDocument probe = new WrapperDocument();
155156
probe.flatDoc = new FlatDocument();
@@ -164,7 +165,7 @@ public void exampleShouldBeMappedAsFlatMapWhenGivenNestedElementsWithLenienMatch
164165
* @see DATAMONGO-1245
165166
*/
166167
@Test
167-
public void exampleShouldBeMappedAsExactObjectWhenGivenNestedElementsWithStriktMatchMode() {
168+
public void exampleShouldBeMappedAsExactObjectWhenGivenNestedElementsWithStrictMatchMode() {
168169

169170
WrapperDocument probe = new WrapperDocument();
170171
probe.flatDoc = new FlatDocument();
@@ -196,6 +197,26 @@ public void exampleShouldBeMappedCorrectlyForFlatTypeWhenStringMatchModeIsStarti
196197
.add("intValue", 100).get()));
197198
}
198199

200+
201+
/**
202+
* @see DATAMONGO-1245
203+
*/
204+
@Test
205+
public void exampleShouldBeMappedCorrectlyForFlatTypeContainingDotsWhenStringMatchModeIsStarting() {
206+
207+
FlatDocument probe = new FlatDocument();
208+
probe.stringValue = "fire.ight";
209+
probe.intValue = 100;
210+
211+
Example<?> example = newExampleOf(probe).matchStringsStartingWith().get();
212+
213+
DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class));
214+
215+
assertThat(dbo,
216+
is(new BasicDBObjectBuilder().add("stringValue", new BasicDBObject("$regex", "^" + Pattern.quote("fire.ight")))
217+
.add("intValue", 100).get()));
218+
}
219+
199220
/**
200221
* @see DATAMONGO-1245
201222
*/
@@ -215,6 +236,25 @@ public void exampleShouldBeMappedCorrectlyForFlatTypeWhenStringMatchModeIsEnding
215236
.add("intValue", 100).get()));
216237
}
217238

239+
/**
240+
* @see DATAMONGO-1245
241+
*/
242+
@Test
243+
public void exampleShouldBeMappedCorrectlyForFlatTypeWhenStringMatchModeRegex() {
244+
245+
FlatDocument probe = new FlatDocument();
246+
probe.stringValue = "firefight";
247+
probe.customNamedField = "^(cat|dog).*shelter\\d?";
248+
249+
Example<?> example = newExampleOf(probe).matchStrings(StringMatcher.REGEX).get();
250+
251+
DBObject dbo = mapper.getMappedExample(example, context.getPersistentEntity(FlatDocument.class));
252+
253+
assertThat(dbo,
254+
is(new BasicDBObjectBuilder().add("stringValue", new BasicDBObject("$regex", "firefight"))
255+
.add("custom_field_name", new BasicDBObject("$regex", "^(cat|dog).*shelter\\d?")).get()));
256+
}
257+
218258
/**
219259
* @see DATAMONGO-1245
220260
*/

0 commit comments

Comments
 (0)