From ba81f21aba589d1124abb2bdf713969dd031299c Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 25 Jun 2012 13:07:10 +0200 Subject: [PATCH] DATAMONGO-467 - Fix identifier handling for Querydsl. As we try to massage the value of the id property into an ObjectId if possible we need to do so as well when mapping the Querydsl query. Adapted SpringDataMongoDbSerializer accordingly. --- .../support/QueryDslMongoRepository.java | 7 ++++++ .../SpringDataMongodbSerializerUnitTests.java | 23 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java index 8980ddabf..4370c31f6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java @@ -28,6 +28,7 @@ import org.springframework.data.domain.Sort.Order; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.QueryMapper; import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; @@ -235,6 +236,7 @@ public class QueryDslMongoRepository extends SimpleM private final MongoConverter converter; private final MappingContext, MongoPersistentProperty> mappingContext; + private final QueryMapper mapper; /** * Creates a new {@link SpringDataMongodbSerializer} for the given {@link MappingContext}. @@ -244,6 +246,7 @@ public class QueryDslMongoRepository extends SimpleM public SpringDataMongodbSerializer(MongoConverter converter) { this.mappingContext = converter.getMappingContext(); this.converter = converter; + this.mapper = new QueryMapper(converter); } @Override @@ -258,6 +261,10 @@ public class QueryDslMongoRepository extends SimpleM @Override protected DBObject asDBObject(String key, Object value) { + if ("_id".equals(key)) { + return super.asDBObject(key, mapper.convertId(value)); + } + return super.asDBObject(key, value instanceof Pattern ? value : converter.convertToMongoType(value)); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java index db2d8ed99..56505eb54 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-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. @@ -18,6 +18,7 @@ package org.springframework.data.mongodb.repository.support; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import org.bson.types.ObjectId; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -34,6 +35,8 @@ import org.springframework.data.mongodb.repository.support.QueryDslMongoReposito import com.mongodb.BasicDBObject; import com.mongodb.DBObject; +import com.mysema.query.types.expr.BooleanOperation; +import com.mysema.query.types.path.PathBuilder; import com.mysema.query.types.path.StringPath; /** @@ -98,7 +101,25 @@ public class SpringDataMongodbSerializerUnitTests { assertThat(serializer.getKeyForPath(address, address.getMetadata()), is("")); } + /** + * @see DATAMONGO-467 + */ + @Test + public void convertsIdPropertyCorrectly() { + + ObjectId id = new ObjectId(); + + PathBuilder
builder = new PathBuilder
(Address.class, "address"); + StringPath idPath = builder.getString("id"); + + DBObject result = (DBObject) serializer.visit((BooleanOperation) idPath.eq(id.toString()), (Void) null); + assertThat(result.get("_id"), is(notNullValue())); + assertThat(result.get("_id"), is(instanceOf(ObjectId.class))); + assertThat(result.get("_id"), is((Object) id)); + } + class Address { + String id; String street; @Field("zip_code") String zipCode;