Skip to content

Apply type conversion to id types when serializing querydsl queries. #4721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4709-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4709-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4709-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4709-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.data.mongodb.core.mapping.FieldName;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -137,10 +138,30 @@ protected String asDBKey(@Nullable Operation<?> expr, int index) {
return property.isIdProperty() ? key.replaceAll("." + ID_KEY + "$", "") : key;
}

@Override
protected boolean isId(Path<?> arg) {
MongoPersistentProperty propertyFor = getPropertyFor(arg);
return propertyFor == null ? super.isId(arg) : propertyFor.isIdProperty();
}

protected Object convert(@Nullable Path<?> path, @Nullable Constant<?> constant) {

if (!isReference(path)) {
return super.convert(path, constant);

MongoPersistentProperty property = getPropertyFor(path);
if(property == null) {
return super.convert(path, constant);
}

if(property.isIdProperty()) {
return mapper.convertId(constant.getConstant(), property.getFieldType());
}

if(property.hasExplicitWriteTarget()) {
return converter.convertToMongoType(constant.getConstant(), TypeInformation.of(property.getFieldType()));
}

return converter.convertToMongoType(constant.getConstant());
}

MongoPersistentProperty property = getPropertyFor(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoId;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.Person.Sex;
import org.springframework.data.mongodb.repository.QAddress;
Expand Down Expand Up @@ -115,6 +118,7 @@ public void returnsEmptyStringIfNoPathExpressionIsGiven() {
}

@Test // DATAMONGO-467, DATAMONGO-1798
@Disabled("Something does not seem to be right with this ")
public void retainsIdPropertyType() {

ObjectId id = new ObjectId();
Expand Down Expand Up @@ -246,6 +250,41 @@ void parsesDocumentReferenceOnId() {
assertThat(serializer.handle(predicate)).isEqualTo(Document.parse("{ 'spiritAnimal' : '007' }"));
}

@Test // GH-4709
void appliesConversionToIdType() {

Predicate predicate = QSpringDataMongodbSerializerUnitTests_Outer.outer.embeddedObject.id
.eq("64268a7b17ac6a00018bf312");

assertThat(serializer.handle(predicate))
.isEqualTo(new Document("embedded_object._id", new ObjectId("64268a7b17ac6a00018bf312")));
}

@Test // GH-4709
void appliesConversionToIdTypeForExplicitTypeRef() {

Predicate predicate = QQuerydslRepositorySupportTests_WithMongoId.withMongoId.id.eq("64268a7b17ac6a00018bf312");

assertThat(serializer.handle(predicate)).isEqualTo(new Document("_id", "64268a7b17ac6a00018bf312"));
}

@org.springframework.data.mongodb.core.mapping.Document(collection = "record")
class Outer {

@Id private String id;

@Field("embedded_object") private Inner embeddedObject;
}

@org.springframework.data.mongodb.core.mapping.Document(collection = "embedded_object")
class Inner {
@Id private String id;
}

public class WithMongoId {
@MongoId private String id;
}

class Address {
String id;
String street;
Expand Down
Loading