Skip to content
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

Fix id value conversion when projecting result types. #4525

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.2.0-SNAPSHOT</version>
<version>4.2.x-4524-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.2.0-SNAPSHOT</version>
<version>4.2.x-4524-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.2.0-SNAPSHOT</version>
<version>4.2.x-4524-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.2.0-SNAPSHOT</version>
<version>4.2.x-4524-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ private Object readIdValue(ConversionContext context, SpELExpressionEvaluator ev
String expression = idProperty.getSpelExpression();
Object resolvedValue = expression != null ? evaluator.evaluate(expression) : rawId;

return resolvedValue != null ? readValue(context, resolvedValue, idProperty.getTypeInformation()) : null;
return resolvedValue != null ? readValue(context.forProperty(idProperty), resolvedValue, idProperty.getTypeInformation()) : null;
}

private void readProperties(ConversionContext context, MongoPersistentEntity<?> entity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -80,6 +82,7 @@
import org.springframework.data.mongodb.core.mapping.FieldName.Type;
import org.springframework.data.mongodb.core.mapping.FieldType;
import org.springframework.data.mongodb.core.mapping.MongoField;
import org.springframework.data.mongodb.core.mapping.MongoId;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.PersonPojoStringId;
Expand Down Expand Up @@ -2955,6 +2958,27 @@ void readShouldAllowDotsInMapKeyNameIfConfigured() {
assertThat(target.mapOfPersons).containsEntry("map.key.with.dots", person);
}

@ValueSource(classes = { ComplexIdAndNoAnnotation.class, ComplexIdAndIdAnnotation.class, ComplexIdAndMongoIdAnnotation.class, ComplexIdAndFieldAnnotation.class })
@ParameterizedTest // GH-4524
void projectShouldReadComplexIdType(Class<?> projectionTargetType) {

EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(),
EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy()
.and((target, underlyingType) -> !converter.conversions.isSimpleType(target)),
mappingContext);

ComplexId idValue = ComplexId.of(101L);
org.bson.Document source = new org.bson.Document("_id", new org.bson.Document("innerId", idValue.innerId))
.append("value", "abc").append("_class", ComplexIdAndNoAnnotation.class.getName());

EntityProjection<?, ComplexIdAndNoAnnotation> projection = introspector.introspect(projectionTargetType,
ComplexIdAndNoAnnotation.class);

assertThat(converter.project(projection, source)) //
.isInstanceOf(projectionTargetType) //
.extracting("id").isEqualTo(idValue);
}

org.bson.Document write(Object source) {

org.bson.Document target = new org.bson.Document();
Expand Down Expand Up @@ -3219,7 +3243,33 @@ static class ClassWithComplexId {
}

static class ComplexId {

Long innerId;

static ComplexId of(Long value) {

ComplexId id = new ComplexId();
id.innerId = value;
return id;
}

@Override
public boolean equals(Object o) {

if (o == this) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ComplexId complexId = (ComplexId) o;
return Objects.equals(innerId, complexId.innerId);
}

@Override
public int hashCode() {
return Objects.hash(innerId);
}
}

static class TypWithCollectionConstructor {
Expand Down Expand Up @@ -4031,7 +4081,32 @@ static class WithPropertyHavingDotsInFieldName {

@Field(name = "field.name.with.dots", nameType = Type.KEY)
String value;
}

static class ComplexIdAndFieldAnnotation {

@Field("_id") //
ComplexId id;
String value;
}

static class ComplexIdAndMongoIdAnnotation {

@MongoId //
ComplexId id;
String value;
}

static class ComplexIdAndIdAnnotation {

@Id //
ComplexId id;
String value;
}

static class ComplexIdAndNoAnnotation {

ComplexId id;
String value;
}
}
Loading