-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Binary
not deserialized to byte[]
for property of type Object
#3670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Do you have the test handy? A complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem helps us a lot identify the issue. |
@christophstrobl Here you go..
public static class TestDomainObject<V> {
protected String foo;
protected V value;
@Override
public String toString() {
return "TestDomainObject{" +
"foo='" + foo + '\'' +
", value=" + value +
'}';
}
}
@Test
public void testBrokenExample() {
ReactiveMongoOperations mongo = injector.getInstance(ReactiveMongoOperations.class);
Query query = new Query(where("foo").is("bar"));
mongo.upsert(query, new Update().set("value", new byte[] {0x00, 0x42}), "exampleCollection").block();
TestDomainObject<?> out = mongo.findOne(query, TestDomainObject.class, "exampleCollection").block();
System.out.println("OUT: " + out + " -> SAME: " + deepEquals(new byte[] {0x00, 0x42}, out.value));
} |
Thanks for the feedback. public static class TypedDomainObject extends TestDomainObject<byte[]> {}
// ...
TestDomainObject<?> out = template.findOne(query, TypedDomainObject.class, "exampleCollection"); Maybe I'm missing something here, in that case please provide a sample GH project with all required dependencies, and we'll be happy to have another look. |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue. |
@otasman Could use your help putting together a mini gradle/intellij project or something that replicates the issue so they can replicate it. |
@christophstrobl main import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.bson.types.Binary;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.Collections;
import java.util.List;
import static java.util.Objects.deepEquals;
import static org.springframework.data.mongodb.core.query.Criteria.where;
public class main {
private static final String connectionString = <CONNECTION STRING>;
private static final String databaseName = "unitTestDb";
@ReadingConverter
public static class BinaryDecoder implements Converter<Binary, Object> {
@Override
public byte[] convert(Binary source) {
return source.getData();
}
}
public static void main(String[] args) {
MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(connectionString))
.build()
);
// Build spring operations with custom converters
ReactiveMongoOperations mongo = new ReactiveMongoTemplate(mongoClient, databaseName);
MappingMongoConverter converter = (MappingMongoConverter) mongo.getConverter();
List<?> customConversions = Collections.singletonList(new BinaryDecoder());
converter.setCustomConversions(new MongoCustomConversions(customConversions));
converter.afterPropertiesSet();
Query query = new Query(where("foo").is("bar"));
mongo.upsert(query, new Update().set("value", new byte[] {0x00, 0x42}), "exampleCollection").block();
TestDomainObject<?> out = mongo.findOne(query, TestDomainObject.class, "exampleCollection").block();
System.out.println("OUT: " + out + " -> SAME: " + deepEquals(new byte[] {0x00, 0x42}, out.value));
System.exit(0);
}
public static class TestDomainObject<V> {
protected String foo;
protected V value;
@Override
public String toString() {
return "TestDomainObject{" +
"foo='" + foo + '\'' +
", value=" + value +
'}';
}
}
} build.gradle
|
@christophstrobl @spring-projects-issues |
@otasman thanks for the sample. I see the custom |
Binary
not deserialized to byte[]
for property of type Object
When upgrading to the latest driver release (3.1.7 -> 3.2.1), our unit tests break due to improper deserialization of type org.bson.types.Binary. For some reason it is no longer being deserialized to a byte[] and left as the Binary class.
The text was updated successfully, but these errors were encountered: