Skip to content

Commit ef34770

Browse files
committed
DATAMONGO-1324 - Register ObjectId converters unconditionally to make sure they really get used.
The presence of ObjectToObjectConverter in a DefaultConversionService causes the guard trying to register converters for ObjectIds in AbstractMongoConverter to not trigger the registration. This in turn caused ObjectId conversions to be executed via reflection instead of the straight forward method calls and thus a drop in performance for such operations. We no unconditionally register the converters to make sure they really get applied. Related tickets: SPR-13703.
1 parent 9dce117 commit ef34770

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,13 @@ public void setInstantiators(EntityInstantiators instantiators) {
7575
*/
7676
private void initializeConverters() {
7777

78-
if (!conversionService.canConvert(ObjectId.class, String.class)) {
79-
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
80-
}
81-
if (!conversionService.canConvert(String.class, ObjectId.class)) {
82-
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
83-
}
78+
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
79+
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
80+
8481
if (!conversionService.canConvert(ObjectId.class, BigInteger.class)) {
8582
conversionService.addConverter(ObjectIdToBigIntegerConverter.INSTANCE);
8683
}
84+
8785
if (!conversionService.canConvert(BigInteger.class, ObjectId.class)) {
8886
conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE);
8987
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2015 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.mockito.Mockito.*;
19+
20+
import org.junit.Test;
21+
import org.springframework.core.convert.support.DefaultConversionService;
22+
import org.springframework.core.convert.support.GenericConversionService;
23+
import org.springframework.data.mapping.context.MappingContext;
24+
import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToStringConverter;
25+
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToObjectIdConverter;
26+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
27+
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
28+
import org.springframework.data.util.TypeInformation;
29+
30+
import com.mongodb.DBObject;
31+
import com.mongodb.DBRef;
32+
33+
/**
34+
* Unit tests for {@link AbstractMongoConverter}.
35+
*
36+
* @author Oliver Gierke
37+
*/
38+
public class AbstractMongoConverterUnitTests {
39+
40+
/**
41+
* @see DATAMONGO-1324
42+
*/
43+
@Test
44+
public void registersObjectIdConvertersExplicitly() {
45+
46+
DefaultConversionService conversionService = spy(new DefaultConversionService());
47+
48+
new SampleMongoConverter(conversionService).afterPropertiesSet();
49+
50+
verify(conversionService).addConverter(StringToObjectIdConverter.INSTANCE);
51+
verify(conversionService).addConverter(ObjectIdToStringConverter.INSTANCE);
52+
}
53+
54+
static class SampleMongoConverter extends AbstractMongoConverter {
55+
56+
public SampleMongoConverter(GenericConversionService conversionService) {
57+
super(conversionService);
58+
}
59+
60+
@Override
61+
public MongoTypeMapper getTypeMapper() {
62+
throw new UnsupportedOperationException();
63+
}
64+
65+
@Override
66+
public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> getMappingContext() {
67+
throw new UnsupportedOperationException();
68+
}
69+
70+
@Override
71+
public <R> R read(Class<R> type, DBObject source) {
72+
throw new UnsupportedOperationException();
73+
}
74+
75+
@Override
76+
public void write(Object source, DBObject sink) {
77+
throw new UnsupportedOperationException();
78+
}
79+
80+
@Override
81+
public Object convertToMongoType(Object obj, TypeInformation<?> typeInformation) {
82+
throw new UnsupportedOperationException();
83+
}
84+
85+
@Override
86+
public DBRef toDBRef(Object object, MongoPersistentProperty referingProperty) {
87+
throw new UnsupportedOperationException();
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)