Skip to content

Commit 6ef518e

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1053 - Type check is now only performed on explicit language properties.
We now only perform a type check on via @language explicitly defined language properties. Prior to this change non-String properties named language caused errors on entity validation. Original pull request: #228.
1 parent ddee2fb commit 6ef518e

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public void doWithPersistentProperty(MongoPersistentProperty persistentProperty)
284284

285285
private void potentiallyAssertLanguageType(MongoPersistentProperty persistentProperty) {
286286

287-
if (persistentProperty.isLanguageProperty()) {
287+
if (persistentProperty.isExplicitLanguageProperty()) {
288288
assertPropertyType(persistentProperty, String.class);
289289
}
290290
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntityUnitTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.mockito.Mock;
2525
import org.mockito.runners.MockitoJUnitRunner;
2626
import org.springframework.context.ApplicationContext;
27+
import org.springframework.data.mapping.model.MappingException;
2728
import org.springframework.data.util.ClassTypeInformation;
2829

2930
/**
@@ -36,6 +37,7 @@
3637
public class BasicMongoPersistentEntityUnitTests {
3738

3839
@Mock ApplicationContext context;
40+
@Mock MongoPersistentProperty propertyMock;
3941

4042
@Test
4143
public void subclassInheritsAtDocumentAnnotation() {
@@ -80,6 +82,61 @@ public void shouldDetectLanguageCorrectly() {
8082
assertThat(entity.getLanguage(), is("spanish"));
8183
}
8284

85+
/**
86+
* @see DATAMONGO-1053
87+
*/
88+
@SuppressWarnings({ "unchecked", "rawtypes" })
89+
@Test(expected = MappingException.class)
90+
public void verifyShouldThrowExceptionForInvalidTypeOfExplicitLanguageProperty() {
91+
92+
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
93+
ClassTypeInformation.from(AnyDocument.class));
94+
95+
when(propertyMock.isExplicitLanguageProperty()).thenReturn(true);
96+
when(propertyMock.getActualType()).thenReturn((Class) Number.class);
97+
98+
entity.addPersistentProperty(propertyMock);
99+
entity.verify();
100+
}
101+
102+
/**
103+
* @see DATAMONGO-1053
104+
*/
105+
@SuppressWarnings({ "unchecked", "rawtypes" })
106+
@Test
107+
public void verifyShouldPassForStringAsExplicitLanguageProperty() {
108+
109+
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
110+
ClassTypeInformation.from(AnyDocument.class));
111+
when(propertyMock.isExplicitLanguageProperty()).thenReturn(true);
112+
when(propertyMock.getActualType()).thenReturn((Class) String.class);
113+
entity.addPersistentProperty(propertyMock);
114+
115+
entity.verify();
116+
117+
verify(propertyMock, times(1)).isExplicitLanguageProperty();
118+
verify(propertyMock, times(1)).getActualType();
119+
}
120+
121+
/**
122+
* @see DATAMONGO-1053
123+
*/
124+
@SuppressWarnings({ "unchecked", "rawtypes" })
125+
@Test
126+
public void verifyShouldIgnoreNonExplicitLanguageProperty() {
127+
128+
BasicMongoPersistentEntity<AnyDocument> entity = new BasicMongoPersistentEntity<AnyDocument>(
129+
ClassTypeInformation.from(AnyDocument.class));
130+
when(propertyMock.isExplicitLanguageProperty()).thenReturn(false);
131+
when(propertyMock.getActualType()).thenReturn((Class) Number.class);
132+
entity.addPersistentProperty(propertyMock);
133+
134+
entity.verify();
135+
136+
verify(propertyMock, times(1)).isExplicitLanguageProperty();
137+
verify(propertyMock, never()).getActualType();
138+
}
139+
83140
@Document(collection = "contacts")
84141
class Contact {
85142

@@ -111,4 +168,8 @@ public String getCollectionName() {
111168
static class DocumentWithLanguage {
112169

113170
}
171+
172+
static class AnyDocument {
173+
174+
}
114175
}

0 commit comments

Comments
 (0)