Skip to content

Commit e7220a0

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1485 - Consider potential custom conversion for enums in Querydsl paths.
We now take potential registered converters for enums into account when serializing path expressions via SpringDataMongodbSerializer. Original pull request: #388.
1 parent f7ab448 commit e7220a0

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2727
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
2828
import org.springframework.util.Assert;
29+
import org.springframework.util.ClassUtils;
2930

3031
import com.mongodb.DBObject;
3132
import com.mongodb.DBRef;
@@ -74,6 +75,20 @@ public SpringDataMongodbSerializer(MongoConverter converter) {
7475
this.mapper = new QueryMapper(converter);
7576
}
7677

78+
/*
79+
* (non-Javadoc)
80+
* @see com.querydsl.mongodb.MongodbSerializer#visit(com.querydsl.core.types.Constant, java.lang.Void)
81+
*/
82+
@Override
83+
public Object visit(Constant<?> expr, Void context) {
84+
85+
if (!ClassUtils.isAssignable(Enum.class, expr.getType())) {
86+
return super.visit(expr, context);
87+
}
88+
89+
return converter.convertToMongoType(expr.getConstant());
90+
}
91+
7792
/*
7893
* (non-Javadoc)
7994
* @see com.mysema.query.mongodb.MongodbSerializer#getKeyForPath(com.mysema.query.types.Path, com.mysema.query.types.PathMetadata)

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,18 +19,24 @@
1919
import static org.junit.Assert.*;
2020
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
2121

22+
import java.util.Collections;
23+
2224
import org.bson.types.ObjectId;
2325
import org.hamcrest.Matchers;
2426
import org.junit.Before;
2527
import org.junit.Test;
2628
import org.junit.runner.RunWith;
2729
import org.mockito.Mock;
2830
import org.mockito.runners.MockitoJUnitRunner;
31+
import org.springframework.core.convert.converter.Converter;
32+
import org.springframework.data.convert.WritingConverter;
33+
import org.springframework.data.mongodb.core.convert.CustomConversions;
2934
import org.springframework.data.mongodb.core.convert.DbRefResolver;
3035
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
3136
import org.springframework.data.mongodb.core.convert.MongoConverter;
3237
import org.springframework.data.mongodb.core.mapping.Field;
3338
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
39+
import org.springframework.data.mongodb.repository.Person.Sex;
3440
import org.springframework.data.mongodb.repository.QAddress;
3541
import org.springframework.data.mongodb.repository.QPerson;
3642

@@ -171,10 +177,52 @@ public void shouldConvertCollectionOfObjectIdEvenWhenNestedInOperatorDbObject()
171177
assertThat($in, Matchers.<Object> arrayContaining(firstId, secondId));
172178
}
173179

180+
/**
181+
* @see DATAMONGO-1485
182+
*/
183+
@Test
184+
public void takesCustomConversionForEnumsIntoAccount() {
185+
186+
MongoMappingContext context = new MongoMappingContext();
187+
188+
MappingMongoConverter converter = new MappingMongoConverter(dbFactory, context);
189+
converter.setCustomConversions(new CustomConversions(Collections.singletonList(new SexTypeWriteConverter())));
190+
converter.afterPropertiesSet();
191+
192+
this.converter = converter;
193+
this.serializer = new SpringDataMongodbSerializer(this.converter);
194+
195+
Object mappedPredicate = this.serializer.handle(QPerson.person.sex.eq(Sex.FEMALE));
196+
197+
assertThat(mappedPredicate, is(instanceOf(DBObject.class)));
198+
assertThat(((DBObject) mappedPredicate).get("sex"), is((Object) "f"));
199+
}
200+
174201
class Address {
175202
String id;
176203
String street;
177204
@Field("zip_code") String zipCode;
178205
@Field("bar") String[] foo;
179206
}
207+
208+
@WritingConverter
209+
public class SexTypeWriteConverter implements Converter<Sex, String> {
210+
211+
@Override
212+
public String convert(Sex source) {
213+
214+
if (source == null) {
215+
return null;
216+
}
217+
218+
switch (source) {
219+
case MALE:
220+
return "m";
221+
case FEMALE:
222+
return "f";
223+
default:
224+
throw new IllegalArgumentException("o_O");
225+
}
226+
}
227+
}
180228
}

0 commit comments

Comments
 (0)