Skip to content

Commit 885c1b0

Browse files
committed
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.
1 parent c8bb46f commit 885c1b0

File tree

6 files changed

+72
-90
lines changed

6 files changed

+72
-90
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@
3636
import org.springframework.data.mapping.model.SimpleTypeHolder;
3737
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
3838
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter;
39-
import org.springframework.data.mongodb.core.convert.MongoConverters.BinaryToUUIDConverter;
4039
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
4140
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
42-
import org.springframework.data.mongodb.core.convert.MongoConverters.UUIDToBinaryConverter;
4341
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
4442
import org.springframework.util.Assert;
4543

@@ -91,8 +89,6 @@ public CustomConversions(List<?> converters) {
9189
this.converters.add(StringToBigDecimalConverter.INSTANCE);
9290
this.converters.add(BigIntegerToStringConverter.INSTANCE);
9391
this.converters.add(StringToBigIntegerConverter.INSTANCE);
94-
this.converters.add(UUIDToBinaryConverter.INSTANCE);
95-
this.converters.add(BinaryToUUIDConverter.INSTANCE);
9692
this.converters.addAll(converters);
9793

9894
for (Object c : this.converters) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,11 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18-
import java.io.UnsupportedEncodingException;
1918
import java.math.BigDecimal;
2019
import java.math.BigInteger;
21-
import java.util.UUID;
2220

23-
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
25-
import org.bson.BSON;
26-
import org.bson.types.Binary;
2721
import org.bson.types.ObjectId;
2822
import org.springframework.core.convert.converter.Converter;
29-
import org.springframework.data.mapping.model.MappingException;
3023
import org.springframework.util.StringUtils;
3124

3225
/**
@@ -126,52 +119,4 @@ public BigInteger convert(String source) {
126119
return StringUtils.hasText(source) ? new BigInteger(source) : null;
127120
}
128121
}
129-
130-
/**
131-
* Custom {@link Converter} to convert {@link UUID}s into {@link Binary}s.
132-
*
133-
* @author Oliver Gierke
134-
*/
135-
public static enum UUIDToBinaryConverter implements Converter<UUID, Binary> {
136-
137-
INSTANCE;
138-
139-
/*
140-
* (non-Javadoc)
141-
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
142-
*/
143-
public Binary convert(UUID source) {
144-
145-
try {
146-
return source == null ? null : new Binary(BSON.B_UUID, source.toString().getBytes("UTF-8"));
147-
} catch (UnsupportedEncodingException e) {
148-
throw new MappingException(String.format("Could nor convert UUID %s into Binary!", source.toString()), e);
149-
}
150-
}
151-
}
152-
153-
public static enum BinaryToUUIDConverter implements Converter<Binary, UUID> {
154-
155-
INSTANCE;
156-
157-
private static final Log LOG = LogFactory.getLog(BinaryToUUIDConverter.class);
158-
159-
/*
160-
* (non-Javadoc)
161-
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
162-
*/
163-
public UUID convert(Binary source) {
164-
165-
if (BSON.B_UUID != source.getType()) {
166-
LOG.warn(String.format("Source binary %s is not an UUID actually! Trying to read it nevertheless...",
167-
source.toString()));
168-
}
169-
170-
try {
171-
return source == null ? null : UUID.fromString(new String(source.getData(), "UTF-8"));
172-
} catch (UnsupportedEncodingException e) {
173-
throw new MappingException(String.format("Could not convert Binary %s into UUID!", source.toString()), e);
174-
}
175-
}
176-
}
177122
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.HashSet;
2121
import java.util.Set;
22+
import java.util.UUID;
2223
import java.util.regex.Pattern;
2324

2425
import org.bson.types.Binary;
@@ -52,6 +53,7 @@ public abstract class MongoSimpleTypes {
5253
simpleTypes.add(DBObject.class);
5354
simpleTypes.add(Pattern.class);
5455
simpleTypes.add(Binary.class);
56+
simpleTypes.add(UUID.class);
5557
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
5658
}
5759

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/CustomConversionsUnitTests.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,6 @@ public void considersBinaryASimpleType() {
152152
assertThat(conversions.isSimpleType(Binary.class), is(true));
153153
}
154154

155-
/**
156-
* @see DATAMONGO-390
157-
*/
158-
@Test
159-
public void convertsUUIDsToBinaryByDefault() {
160-
161-
CustomConversions conversions = new CustomConversions();
162-
assertThat(conversions.hasCustomWriteTarget(UUID.class), is(true));
163-
}
164-
165155
enum FormatToStringConverter implements Converter<Format, String> {
166156
INSTANCE;
167157

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import java.util.UUID;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.data.mongodb.core.MongoOperations;
28+
import org.springframework.data.mongodb.core.query.Criteria;
29+
import org.springframework.data.mongodb.core.query.Query;
30+
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
33+
/**
34+
* Integration tests for {@link MongoConverters}.
35+
*
36+
* @author Oliver Gierke
37+
*/
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@ContextConfiguration("classpath:infrastructure.xml")
40+
public class MongoConvertersIntegrationTests {
41+
42+
static final String COLLECTION = "_sample";
43+
44+
@Autowired
45+
MongoOperations template;
46+
47+
@Before
48+
public void setUp() {
49+
template.dropCollection(COLLECTION);
50+
}
51+
52+
@Test
53+
public void writesUUIDBinaryCorrectly() {
54+
55+
Wrapper wrapper = new Wrapper();
56+
wrapper.uuid = UUID.randomUUID();
57+
template.save(wrapper);
58+
59+
assertThat(wrapper.id, is(notNullValue()));
60+
61+
Wrapper result = template.findOne(Query.query(Criteria.where("id").is(wrapper.id)), Wrapper.class);
62+
assertThat(result.uuid, is(wrapper.uuid));
63+
}
64+
65+
static class Wrapper {
66+
67+
String id;
68+
UUID uuid;
69+
}
70+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersUnitTests.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@
1919
import static org.junit.Assert.*;
2020

2121
import java.math.BigDecimal;
22-
import java.util.UUID;
2322

24-
import org.bson.BSON;
25-
import org.bson.types.Binary;
2623
import org.junit.Test;
2724
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
28-
import org.springframework.data.mongodb.core.convert.MongoConverters.BinaryToUUIDConverter;
2925
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
30-
import org.springframework.data.mongodb.core.convert.MongoConverters.UUIDToBinaryConverter;
3126

3227
/**
3328
* Unit tests for {@link MongoConverters}.
@@ -46,20 +41,4 @@ public void convertsBigDecimalToStringAndBackCorrectly() {
4641
BigDecimal reference = StringToBigDecimalConverter.INSTANCE.convert(value);
4742
assertThat(reference, is(bigDecimal));
4843
}
49-
50-
/**
51-
* @see DATAMONGO-390
52-
*/
53-
@Test
54-
public void convertsUUIDToBinaryCorrectly() {
55-
56-
UUID uuid = UUID.randomUUID();
57-
Binary binary = UUIDToBinaryConverter.INSTANCE.convert(uuid);
58-
59-
assertThat(binary, is(notNullValue()));
60-
assertThat(binary.getType(), is(BSON.B_UUID));
61-
62-
UUID result = BinaryToUUIDConverter.INSTANCE.convert(binary);
63-
assertThat(result, is(uuid));
64-
}
6544
}

0 commit comments

Comments
 (0)