Skip to content

DATAMONGO-2193 - Fix String <> ObjectId conversion for non id properties. #640

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>2.1.5.BUILD-SNAPSHOT</version>
<version>2.1.x.DATAMONGO-2193-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>2.1.5.BUILD-SNAPSHOT</version>
<version>2.1.x.DATAMONGO-2193-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.5.BUILD-SNAPSHOT</version>
<version>2.1.x.DATAMONGO-2193-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.1.5.BUILD-SNAPSHOT</version>
<version>2.1.x.DATAMONGO-2193-SNAPSHOT</version>
</dependency>

<!-- reactive -->
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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.5.BUILD-SNAPSHOT</version>
<version>2.1.x.DATAMONGO-2193-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 @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.1.5.BUILD-SNAPSHOT</version>
<version>2.1.x.DATAMONGO-2193-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,11 @@ public MetadataBackedField with(String name) {
@Override
public boolean isIdField() {

MongoPersistentProperty idProperty = (property != null && property.isIdProperty()) ? property
: entity.getIdProperty();
if(property != null) {
return property.isIdProperty();
}

MongoPersistentProperty idProperty = entity.getIdProperty();

if (idProperty != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ protected void cleanDb() {
template.dropCollection(WithGeoJson.class);
template.dropCollection(DocumentWithNestedTypeHavingStringIdProperty.class);
template.dropCollection(ImmutableAudited.class);
template.dropCollection(Outer.class);
}

@Test
Expand Down Expand Up @@ -3663,6 +3664,24 @@ public void writesAuditingMetadataForImmutableTypes() {
assertThat(read.modified).isEqualTo(result.modified).describedAs("Expected auditing information to be read!");
}

@Test // DATAMONGO-2193
public void shouldNotConvertStringToObjectIdForNonIdField() {

ObjectId outerId = new ObjectId();
String innerId = new ObjectId().toHexString();

org.bson.Document source = new org.bson.Document() //
.append("_id", outerId) //
.append("inner", new org.bson.Document("id", innerId).append("value", "boooh"));

template.getDb().getCollection(template.getCollectionName(Outer.class)).insertOne(source);

Outer target = template.findOne(query(where("inner.id").is(innerId)), Outer.class);
assertThat(target).isNotNull();
assertThat(target.id).isEqualTo(outerId);
assertThat(target.inner.id).isEqualTo(innerId);
}

private AtomicReference<ImmutableVersioned> createAfterSaveReference() {

AtomicReference<ImmutableVersioned> saved = new AtomicReference<>();
Expand Down Expand Up @@ -4178,4 +4197,16 @@ static class ImmutableAudited {
@Id String id;
@LastModifiedDate Instant modified;
}

static class Outer {

@Id ObjectId id;
Inner inner;
}

static class Inner {

@Field("id") String id;
String value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,18 @@ public void getMappedObjectShouldNotMapTypeHint() {
assertThat(mappedObject).containsEntry("className", "foo");
}

@Test // DATAMONGO-2193
public void shouldNotConvertHexStringToObjectIdForRenamedNestedIdField() {

String idHex = new ObjectId().toHexString();
Query query = new Query(where("nested.id").is(idHex));

org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(RootForClassWithExplicitlyRenamedIdField.class));

assertThat(document).isEqualTo(new org.bson.Document("nested.id", idHex));
}

@Document
public class Foo {
@Id private ObjectId id;
Expand Down