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.
This commit is contained in:
committed by
Oliver Gierke
parent
be65970710
commit
d760d9cc11
@@ -21,6 +21,9 @@ import java.util.List;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
import org.bson.BSON;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.data.mongodb.core.MongoOperations;
|
import org.springframework.data.mongodb.core.MongoOperations;
|
||||||
@@ -224,6 +227,15 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
|
|||||||
return (String) value;
|
return (String) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value instanceof byte[]) {
|
||||||
|
|
||||||
|
String base64representation = DatatypeConverter.printBase64Binary((byte[]) value);
|
||||||
|
if (!binding.isQuoted()) {
|
||||||
|
return "{ '$binary' : '" + base64representation + "', '$type' : " + BSON.B_GENERAL + "}";
|
||||||
|
}
|
||||||
|
return base64representation;
|
||||||
|
}
|
||||||
|
|
||||||
return JSON.serialize(value);
|
return JSON.serialize(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
import org.bson.BSON;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -343,6 +346,23 @@ public class StringBasedMongoQueryUnitTests {
|
|||||||
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1290
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldSupportNonQuotedBinaryDataReplacement() throws Exception {
|
||||||
|
|
||||||
|
byte[] binaryData = "Matthews".getBytes("UTF-8");
|
||||||
|
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, binaryData);
|
||||||
|
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByLastnameAsBinary", byte[].class);
|
||||||
|
|
||||||
|
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
|
||||||
|
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery("{'lastname' : { '$binary' : '"
|
||||||
|
+ DatatypeConverter.printBase64Binary(binaryData) + "', '$type' : " + BSON.B_GENERAL + "}}");
|
||||||
|
|
||||||
|
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
||||||
|
}
|
||||||
|
|
||||||
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
|
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
|
||||||
|
|
||||||
Method method = SampleRepository.class.getMethod(name, parameters);
|
Method method = SampleRepository.class.getMethod(name, parameters);
|
||||||
@@ -355,6 +375,9 @@ public class StringBasedMongoQueryUnitTests {
|
|||||||
@Query("{ 'lastname' : ?0 }")
|
@Query("{ 'lastname' : ?0 }")
|
||||||
Person findByLastname(String lastname);
|
Person findByLastname(String lastname);
|
||||||
|
|
||||||
|
@Query("{ 'lastname' : ?0 }")
|
||||||
|
Person findByLastnameAsBinary(byte[] lastname);
|
||||||
|
|
||||||
@Query("{ 'lastname' : '?0' }")
|
@Query("{ 'lastname' : '?0' }")
|
||||||
Person findByLastnameQuoted(String lastname);
|
Person findByLastnameQuoted(String lastname);
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ Import-Template:
|
|||||||
javax.tools.*;version="0",
|
javax.tools.*;version="0",
|
||||||
javax.net.*;version="0",
|
javax.net.*;version="0",
|
||||||
javax.validation.*;version="${validation:[=.=.=.=,+1.0.0)}";resolution:=optional,
|
javax.validation.*;version="${validation:[=.=.=.=,+1.0.0)}";resolution:=optional,
|
||||||
|
javax.xml.bind.*;version=0,
|
||||||
org.aopalliance.*;version="[1.0.0, 2.0.0)";resolution:=optional,
|
org.aopalliance.*;version="[1.0.0, 2.0.0)";resolution:=optional,
|
||||||
org.bson.*;version="0",
|
org.bson.*;version="0",
|
||||||
org.objenesis.*;version="${objenesis:[=.=.=, +1.0.0)}";resolution:=optional,
|
org.objenesis.*;version="${objenesis:[=.=.=, +1.0.0)}";resolution:=optional,
|
||||||
|
|||||||
Reference in New Issue
Block a user