Skip to content

Commit e30eeaa

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1290 - Convert byte[] parameter in @query to $binary representation.
We now convert non quoted binary parameters to the $binary format. This allows using them along with the @query annotation. Original pull request: #332.
1 parent b8cb2f7 commit e30eeaa

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQuery.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import java.util.regex.Matcher;
2222
import java.util.regex.Pattern;
2323

24+
import javax.xml.bind.DatatypeConverter;
25+
26+
import org.bson.BSON;
2427
import org.slf4j.Logger;
2528
import org.slf4j.LoggerFactory;
2629
import org.springframework.data.mongodb.core.MongoOperations;
@@ -176,6 +179,15 @@ private String getParameterValueForBinding(ConvertingParameterAccessor accessor,
176179
return (String) value;
177180
}
178181

182+
if (value instanceof byte[]) {
183+
184+
String base64representation = DatatypeConverter.printBase64Binary((byte[]) value);
185+
if (!binding.isQuoted()) {
186+
return "{ '$binary' : '" + base64representation + "', '$type' : " + BSON.B_GENERAL + "}";
187+
}
188+
return base64representation;
189+
}
190+
179191
return JSON.serialize(value);
180192
}
181193

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27+
import javax.xml.bind.DatatypeConverter;
28+
29+
import org.bson.BSON;
2730
import org.junit.Before;
2831
import org.junit.Test;
2932
import org.junit.runner.RunWith;
@@ -289,6 +292,23 @@ public void shouldParseJsonKeyReplacementCorrectly() throws Exception {
289292
assertThat(query.getQueryObject(), is(new BasicDBObjectBuilder().add("key", "value").get()));
290293
}
291294

295+
/**
296+
* @see DATAMONGO-1290
297+
*/
298+
@Test
299+
public void shouldSupportNonQuotedBinaryDataReplacement() throws Exception {
300+
301+
byte[] binaryData = "Matthews".getBytes("UTF-8");
302+
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, binaryData);
303+
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByLastnameAsBinary", byte[].class);
304+
305+
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
306+
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery("{'lastname' : { '$binary' : '"
307+
+ DatatypeConverter.printBase64Binary(binaryData) + "', '$type' : " + BSON.B_GENERAL + "}}");
308+
309+
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
310+
}
311+
292312
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
293313

294314
Method method = SampleRepository.class.getMethod(name, parameters);
@@ -301,6 +321,9 @@ private interface SampleRepository {
301321
@Query("{ 'lastname' : ?0 }")
302322
Person findByLastname(String lastname);
303323

324+
@Query("{ 'lastname' : ?0 }")
325+
Person findByLastnameAsBinary(byte[] lastname);
326+
304327
@Query("{ 'lastname' : '?0' }")
305328
Person findByLastnameQuoted(String lastname);
306329

spring-data-mongodb/template.mf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Import-Template:
1616
javax.tools.*;version="0",
1717
javax.net.*;version="0",
1818
javax.validation.*;version="${validation:[=.=.=.=,+1.0.0)}";resolution:=optional,
19+
javax.xml.bind.*;version=0,
1920
org.aopalliance.*;version="[1.0.0, 2.0.0)";resolution:=optional,
2021
org.bson.*;version="0",
2122
org.objenesis.*;version="${objenesis:[=.=.=, +1.0.0)}";resolution:=optional,

0 commit comments

Comments
 (0)