Skip to content

Apply conversion on document reference lookup pointing directly to the id property. #4044

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 2 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.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4033-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.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4033-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.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4033-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 @@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4033-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,11 @@ protected Object delegateConvertToMongoType(Object source, @Nullable MongoPersis
}

protected Object convertAssociation(Object source, Field field) {
return convertAssociation(source, field.getProperty());
Object value = convertAssociation(source, field.getProperty());
if(value != null && field.isIdField() && field.getFieldType() != value.getClass()) {
return convertId(value, field.getFieldType());
}
return value;
}

/**
Expand Down Expand Up @@ -1043,6 +1047,9 @@ public TypeInformation<?> getTypeHint() {
return ClassTypeInformation.OBJECT;
}

public Class<?> getFieldType() {
return Object.class;
}
}

/**
Expand Down Expand Up @@ -1171,6 +1178,11 @@ private Association<MongoPersistentProperty> findAssociation() {
return null;
}

@Override
public Class<?> getFieldType() {
return property.getFieldType();
}

@Override
public String getMappedKey() {
return path == null ? name : path.toDotPath(isAssociation() ? getAssociationConverter() : getPropertyConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,31 @@ void convertsDocumentReferenceOnIdPropertyCorrectly() {
assertThat(mappedQuery).containsEntry("sample", "s1");
}

@Test // GH-4033
void convertsNestedPathToIdPropertyOfDocumentReferenceCorrectly() {

Query query = query(where("sample.foo").is("s1"));
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedQuery).containsEntry("sample", "s1");
}

@Test // GH-4033
void convertsNestedPathToIdPropertyOfDocumentReferenceCorrectlyWhenItShouldBeConvertedToObjectId() {

ObjectId id = new ObjectId();
Query query = query(where("sample.foo").is(id.toHexString()));
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(WithDocumentReference.class));

assertThat(mappedQuery.get("sample")).satisfies(it -> {

assertThat(it).isInstanceOf(ObjectId.class);
assertThat(((ObjectId) it).toHexString()).isEqualTo(id.toHexString());
});
}

@Test // GH-3853
void convertsListDocumentReferenceOnIdPropertyCorrectly() {

Expand Down