Skip to content

Commit cbfc462

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: spring-projects#332.
1 parent b31efb4 commit cbfc462

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

+12
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;
@@ -224,6 +227,15 @@ private String getParameterValueForBinding(ConvertingParameterAccessor accessor,
224227
return (String) value;
225228
}
226229

230+
if (value instanceof byte[]) {
231+
232+
String base64representation = DatatypeConverter.printBase64Binary((byte[]) value);
233+
if (!binding.isQuoted()) {
234+
return "{ '$binary' : '" + base64representation + "', '$type' : " + BSON.B_GENERAL + "}";
235+
}
236+
return base64representation;
237+
}
238+
227239
return JSON.serialize(value);
228240
}
229241

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

+23
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;
@@ -343,6 +346,23 @@ public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects() t
343346
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
344347
}
345348

349+
/**
350+
* @see DATAMONGO-1290
351+
*/
352+
@Test
353+
public void shouldSupportNonQuotedBinaryDataReplacement() throws Exception {
354+
355+
byte[] binaryData = "Matthews".getBytes("UTF-8");
356+
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, binaryData);
357+
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByLastnameAsBinary", byte[].class);
358+
359+
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
360+
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery("{'lastname' : { '$binary' : '"
361+
+ DatatypeConverter.printBase64Binary(binaryData) + "', '$type' : " + BSON.B_GENERAL + "}}");
362+
363+
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
364+
}
365+
346366
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
347367

348368
Method method = SampleRepository.class.getMethod(name, parameters);
@@ -355,6 +375,9 @@ private interface SampleRepository {
355375
@Query("{ 'lastname' : ?0 }")
356376
Person findByLastname(String lastname);
357377

378+
@Query("{ 'lastname' : ?0 }")
379+
Person findByLastnameAsBinary(byte[] lastname);
380+
358381
@Query("{ 'lastname' : '?0' }")
359382
Person findByLastnameQuoted(String lastname);
360383

spring-data-mongodb/template.mf

+1
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)