|
30 | 30 | import org.junit.jupiter.api.extension.ExtendWith;
|
31 | 31 | import org.mockito.Mock;
|
32 | 32 | import org.mockito.junit.jupiter.MockitoExtension;
|
| 33 | + |
33 | 34 | import org.springframework.core.convert.converter.Converter;
|
| 35 | +import org.springframework.data.annotation.Id; |
34 | 36 | import org.springframework.data.convert.WritingConverter;
|
35 | 37 | import org.springframework.data.mongodb.core.convert.DbRefResolver;
|
36 | 38 | import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
37 | 39 | import org.springframework.data.mongodb.core.convert.MongoConverter;
|
38 | 40 | import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
|
39 | 41 | import org.springframework.data.mongodb.core.mapping.Field;
|
| 42 | +import org.springframework.data.mongodb.core.mapping.MongoId; |
40 | 43 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
41 | 44 | import org.springframework.data.mongodb.repository.Person.Sex;
|
42 | 45 | import org.springframework.data.mongodb.repository.QAddress;
|
43 | 46 | import org.springframework.data.mongodb.repository.QPerson;
|
| 47 | +import org.springframework.data.mongodb.repository.User; |
44 | 48 |
|
45 | 49 | import com.querydsl.core.types.Ops;
|
46 | 50 | import com.querydsl.core.types.Predicate;
|
|
50 | 54 | import com.querydsl.core.types.dsl.PathBuilder;
|
51 | 55 | import com.querydsl.core.types.dsl.SimplePath;
|
52 | 56 | import com.querydsl.core.types.dsl.StringPath;
|
53 |
| -import org.springframework.data.mongodb.repository.User; |
54 | 57 |
|
55 | 58 | /**
|
56 | 59 | * Unit tests for {@link SpringDataMongodbSerializer}.
|
@@ -115,15 +118,15 @@ public void returnsEmptyStringIfNoPathExpressionIsGiven() {
|
115 | 118 | }
|
116 | 119 |
|
117 | 120 | @Test // DATAMONGO-467, DATAMONGO-1798
|
118 |
| - public void retainsIdPropertyType() { |
| 121 | + public void appliesImplicitIdConversion() { |
119 | 122 |
|
120 | 123 | ObjectId id = new ObjectId();
|
121 | 124 |
|
122 | 125 | PathBuilder<Address> builder = new PathBuilder<Address>(Address.class, "address");
|
123 | 126 | StringPath idPath = builder.getString("id");
|
124 | 127 |
|
125 | 128 | Document result = (Document) serializer.visit((BooleanOperation) idPath.eq(id.toString()), null);
|
126 |
| - assertThat(result.get("_id")).isNotNull().isInstanceOf(String.class).isEqualTo(id.toString()); |
| 129 | + assertThat(result.get("_id")).isNotNull().isInstanceOf(ObjectId.class); |
127 | 130 | }
|
128 | 131 |
|
129 | 132 | @Test // DATAMONGO-761
|
@@ -246,6 +249,41 @@ void parsesDocumentReferenceOnId() {
|
246 | 249 | assertThat(serializer.handle(predicate)).isEqualTo(Document.parse("{ 'spiritAnimal' : '007' }"));
|
247 | 250 | }
|
248 | 251 |
|
| 252 | + @Test // GH-4709 |
| 253 | + void appliesConversionToIdType() { |
| 254 | + |
| 255 | + Predicate predicate = QSpringDataMongodbSerializerUnitTests_Outer.outer.embeddedObject.id |
| 256 | + .eq("64268a7b17ac6a00018bf312"); |
| 257 | + |
| 258 | + assertThat(serializer.handle(predicate)) |
| 259 | + .isEqualTo(new Document("embedded_object._id", new ObjectId("64268a7b17ac6a00018bf312"))); |
| 260 | + } |
| 261 | + |
| 262 | + @Test // GH-4709 |
| 263 | + void appliesConversionToIdTypeForExplicitTypeRef() { |
| 264 | + |
| 265 | + Predicate predicate = QQuerydslRepositorySupportTests_WithMongoId.withMongoId.id.eq("64268a7b17ac6a00018bf312"); |
| 266 | + |
| 267 | + assertThat(serializer.handle(predicate)).isEqualTo(new Document("_id", "64268a7b17ac6a00018bf312")); |
| 268 | + } |
| 269 | + |
| 270 | + @org.springframework.data.mongodb.core.mapping.Document(collection = "record") |
| 271 | + class Outer { |
| 272 | + |
| 273 | + @Id private String id; |
| 274 | + |
| 275 | + @Field("embedded_object") private Inner embeddedObject; |
| 276 | + } |
| 277 | + |
| 278 | + @org.springframework.data.mongodb.core.mapping.Document(collection = "embedded_object") |
| 279 | + class Inner { |
| 280 | + @Id private String id; |
| 281 | + } |
| 282 | + |
| 283 | + public class WithMongoId { |
| 284 | + @MongoId private String id; |
| 285 | + } |
| 286 | + |
249 | 287 | class Address {
|
250 | 288 | String id;
|
251 | 289 | String street;
|
|
0 commit comments