Skip to content

DATAMONGO-1509 - Write type hint as last element of an Document #411

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>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1509-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
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.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1509-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1509-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.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1509-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1509-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.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1509-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,20 +359,20 @@ public void write(final Object obj, final Bson bson) {
return;
}

Class<?> entityType = obj.getClass();
boolean handledByCustomConverter = conversions.getCustomWriteTarget(entityType, Document.class) != null;
Class<?> entityType = ClassUtils.getUserClass(obj.getClass());
TypeInformation<? extends Object> type = ClassTypeInformation.from(entityType);

if (!handledByCustomConverter && !(bson instanceof Collection)) {
typeMapper.writeType(type, bson);
}

Object target = obj instanceof LazyLoadingProxy ? ((LazyLoadingProxy) obj).getTarget() : obj;

writeInternal(target, bson, type);
if (asMap(bson).containsKey("_is") && asMap(bson).get("_id") == null) {
removeFromMap(bson, "_id");
}

boolean handledByCustomConverter = conversions.getCustomWriteTarget(entityType, Document.class) != null;
if (!handledByCustomConverter && !(bson instanceof Collection)) {
typeMapper.writeType(type, bson);
}
}

/**
Expand Down Expand Up @@ -529,12 +529,12 @@ protected void writePropertyInternal(Object obj, Bson bson, MongoPersistentPrope

Object existingValue = accessor.get(prop);
Document document = existingValue instanceof Document ? (Document) existingValue : new Document();
addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, document);

MongoPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
? mappingContext.getPersistentEntity(obj.getClass()) : mappingContext.getPersistentEntity(type);

writeInternal(obj, document, entity);
addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, document);
accessor.put(prop, document);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.Iterator;
import java.util.List;

import org.bson.Document;
Expand Down Expand Up @@ -80,4 +81,23 @@ public static <T> T getTypedValue(Document source, String key, Class<T> type) {

return (T) value;
}

public static void assertTypeHint(Document document, Class<?> type) {
assertTypeHint(document, type.getName());
}

public static void assertTypeHint(Document document, String expectedTypeString) {

Iterator<String> keyIterator = document.keySet().iterator();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
if (key.equals("_class")) {
assertThat((String) document.get(key), is(equalTo(expectedTypeString)));
assertThat(keyIterator.hasNext(), is(false));
return;
}
}

fail(String.format("Expected to find type info %s in %s.", document, expectedTypeString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -3468,6 +3469,22 @@ public void onBeforeSave(BeforeSaveEvent<Document> event) {
assertThat(document.id, is(notNullValue()));
}


/**
* @see DATAMONGO-1509
*/
@Test
public void findsByGnericNestedListElements() {

List<Model> modelList = Arrays.<Model>asList(new ModelA("value"));
DocumentWithCollection dwc = new DocumentWithCollection(modelList);

template.insert(dwc);

Query query = query(where("models").is(modelList));
assertThat(template.findOne(query, DocumentWithCollection.class), is(equalTo(dwc)));
}

static class TypeWithNumbers {

@Id String id;
Expand Down Expand Up @@ -3547,6 +3564,7 @@ static class DocumentWithDBRefCollection {
@org.springframework.data.mongodb.core.mapping.DBRef(lazy = true) public Map<String, Sample> lazyDbRefAnnotatedMap;
}

@EqualsAndHashCode
static class DocumentWithCollection {

@Id String id;
Expand Down Expand Up @@ -3599,6 +3617,7 @@ static interface Model {
String id();
}

@EqualsAndHashCode
static class ModelA implements Model {

@Id String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ public void maybeConvertHandlesNullValuesCorrectly() {
assertThat(converter.convertToMongoType(null), is(nullValue()));
}

/**
* @see DATAMONGO-1509
*/
@Test
public void writesGenericTypeCorrectly() {

Expand All @@ -537,7 +540,7 @@ public void writesGenericTypeCorrectly() {
converter.write(type, result);

org.bson.Document content = (org.bson.Document) result.get("content");
assertThat(content.get("_class"), is(notNullValue()));
assertTypeHint(content, Address.class);
assertThat(content.get("city"), is(notNullValue()));
}

Expand Down Expand Up @@ -1272,6 +1275,7 @@ public void eagerlyReturnsDBRefObjectIfTargetAlreadyIsOne() {

/**
* @see DATAMONGO-523
* @see DATAMONGO-1509
*/
@Test
public void considersTypeAliasAnnotation() {
Expand All @@ -1282,9 +1286,7 @@ public void considersTypeAliasAnnotation() {
org.bson.Document result = new org.bson.Document();
converter.write(aliased, result);

Object type = result.get("_class");
assertThat(type, is(notNullValue()));
assertThat(type.toString(), is("_"));
assertTypeHint(result, "_");
}

/**
Expand Down Expand Up @@ -1409,6 +1411,7 @@ public void writesProjectingTypeCorrectly() {
/**
* @see DATAMONGO-812
* @see DATAMONGO-893
* @see DATAMONGO-1509
*/
@Test
public void convertsListToBasicDBListAndRetainsTypeInformationForComplexObjects() {
Expand All @@ -1424,7 +1427,7 @@ public void convertsListToBasicDBListAndRetainsTypeInformationForComplexObjects(

List<Object> dbList = (List<Object>) result;
assertThat(dbList, hasSize(1));
assertThat(getTypedValue(getAsDocument(dbList, 0), "_class", String.class), equalTo(Address.class.getName()));
assertTypeHint(getAsDocument(dbList, 0), Address.class);
}

/**
Expand All @@ -1444,6 +1447,7 @@ public void convertsListToBasicDBListWithoutTypeInformationForSimpleTypes() {

/**
* @see DATAMONGO-812
* @see DATAMONGO-1509
*/
@Test
public void convertsArrayToBasicDBListAndRetainsTypeInformationForComplexObjects() {
Expand All @@ -1458,7 +1462,7 @@ public void convertsArrayToBasicDBListAndRetainsTypeInformationForComplexObjects

List<Object> dbList = (List<Object>) result;
assertThat(dbList, hasSize(1));
assertThat(getTypedValue(getAsDocument(dbList, 0), "_class", String.class), equalTo(Address.class.getName()));
assertTypeHint(getAsDocument(dbList, 0), Address.class);
}

/**
Expand Down Expand Up @@ -1802,6 +1806,7 @@ public void shouldIncludeTextScorePropertyWhenReading() {

/**
* @see DATAMONGO-1001
* @see DATAMONGO-1509
*/
@Test
public void shouldWriteCglibProxiedClassTypeInformationCorrectly() {
Expand All @@ -1814,7 +1819,7 @@ public void shouldWriteCglibProxiedClassTypeInformationCorrectly() {
org.bson.Document document = new org.bson.Document();
converter.write(proxied, document);

assertThat(document.get("_class"), is((Object) GenericType.class.getName()));
assertTypeHint(document, GenericType.class);
}

/**
Expand Down