DATAMONGO-422 - Fixed invalid UUID conversion.
Removed UUIDToBinaryConverter and BinaryToUUIDConverter as the MongoDB Java driver can handle it itself. Added UUID as Mongo-simple type. Added integration test for reading and writing a UUID property.
This commit is contained in:
@@ -36,10 +36,8 @@ import org.springframework.data.convert.WritingConverter;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.BinaryToUUIDConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.UUIDToBinaryConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -91,8 +89,6 @@ public class CustomConversions {
|
||||
this.converters.add(StringToBigDecimalConverter.INSTANCE);
|
||||
this.converters.add(BigIntegerToStringConverter.INSTANCE);
|
||||
this.converters.add(StringToBigIntegerConverter.INSTANCE);
|
||||
this.converters.add(UUIDToBinaryConverter.INSTANCE);
|
||||
this.converters.add(BinaryToUUIDConverter.INSTANCE);
|
||||
this.converters.addAll(converters);
|
||||
|
||||
for (Object c : this.converters) {
|
||||
|
||||
@@ -15,18 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.convert;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.bson.BSON;
|
||||
import org.bson.types.Binary;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -126,52 +119,4 @@ abstract class MongoConverters {
|
||||
return StringUtils.hasText(source) ? new BigInteger(source) : null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom {@link Converter} to convert {@link UUID}s into {@link Binary}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public static enum UUIDToBinaryConverter implements Converter<UUID, Binary> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||
*/
|
||||
public Binary convert(UUID source) {
|
||||
|
||||
try {
|
||||
return source == null ? null : new Binary(BSON.B_UUID, source.toString().getBytes("UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new MappingException(String.format("Could nor convert UUID %s into Binary!", source.toString()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static enum BinaryToUUIDConverter implements Converter<Binary, UUID> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(BinaryToUUIDConverter.class);
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||
*/
|
||||
public UUID convert(Binary source) {
|
||||
|
||||
if (BSON.B_UUID != source.getType()) {
|
||||
LOG.warn(String.format("Source binary %s is not an UUID actually! Trying to read it nevertheless...",
|
||||
source.toString()));
|
||||
}
|
||||
|
||||
try {
|
||||
return source == null ? null : UUID.fromString(new String(source.getData(), "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new MappingException(String.format("Could not convert Binary %s into UUID!", source.toString()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.math.BigInteger;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bson.types.Binary;
|
||||
@@ -52,6 +53,7 @@ public abstract class MongoSimpleTypes {
|
||||
simpleTypes.add(DBObject.class);
|
||||
simpleTypes.add(Pattern.class);
|
||||
simpleTypes.add(Binary.class);
|
||||
simpleTypes.add(UUID.class);
|
||||
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
|
||||
}
|
||||
|
||||
|
||||
@@ -152,16 +152,6 @@ public class CustomConversionsUnitTests {
|
||||
assertThat(conversions.isSimpleType(Binary.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-390
|
||||
*/
|
||||
@Test
|
||||
public void convertsUUIDsToBinaryByDefault() {
|
||||
|
||||
CustomConversions conversions = new CustomConversions();
|
||||
assertThat(conversions.hasCustomWriteTarget(UUID.class), is(true));
|
||||
}
|
||||
|
||||
enum FormatToStringConverter implements Converter<Format, String> {
|
||||
INSTANCE;
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.convert;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link MongoConverters}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:infrastructure.xml")
|
||||
public class MongoConvertersIntegrationTests {
|
||||
|
||||
static final String COLLECTION = "_sample";
|
||||
|
||||
@Autowired
|
||||
MongoOperations template;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
template.dropCollection(COLLECTION);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writesUUIDBinaryCorrectly() {
|
||||
|
||||
Wrapper wrapper = new Wrapper();
|
||||
wrapper.uuid = UUID.randomUUID();
|
||||
template.save(wrapper);
|
||||
|
||||
assertThat(wrapper.id, is(notNullValue()));
|
||||
|
||||
Wrapper result = template.findOne(Query.query(Criteria.where("id").is(wrapper.id)), Wrapper.class);
|
||||
assertThat(result.uuid, is(wrapper.uuid));
|
||||
}
|
||||
|
||||
static class Wrapper {
|
||||
|
||||
String id;
|
||||
UUID uuid;
|
||||
}
|
||||
}
|
||||
@@ -19,15 +19,10 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bson.BSON;
|
||||
import org.bson.types.Binary;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.BinaryToUUIDConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.UUIDToBinaryConverter;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MongoConverters}.
|
||||
@@ -46,20 +41,4 @@ public class MongoConvertersUnitTests {
|
||||
BigDecimal reference = StringToBigDecimalConverter.INSTANCE.convert(value);
|
||||
assertThat(reference, is(bigDecimal));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-390
|
||||
*/
|
||||
@Test
|
||||
public void convertsUUIDToBinaryCorrectly() {
|
||||
|
||||
UUID uuid = UUID.randomUUID();
|
||||
Binary binary = UUIDToBinaryConverter.INSTANCE.convert(uuid);
|
||||
|
||||
assertThat(binary, is(notNullValue()));
|
||||
assertThat(binary.getType(), is(BSON.B_UUID));
|
||||
|
||||
UUID result = BinaryToUUIDConverter.INSTANCE.convert(binary);
|
||||
assertThat(result, is(uuid));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user