Skip to content

Commit 31a4bf9

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-892 - Reject nested MappingMongoConverter declarations in XML.
Mapping information is potentially required by multiple instances and thus must not be registered as nested bean. We now actively check for such an invalid scenario and explicitly reject it. Original pull request: spring-projects#165.
1 parent 599291e commit 31a4bf9

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
* @author Oliver Gierke
7373
* @author Maciej Walkowiak
7474
* @author Thomas Darimont
75+
* @author Christoph Strobl
7576
*/
7677
public class MappingMongoConverterParser implements BeanDefinitionParser {
7778

@@ -84,8 +85,11 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
8485
*/
8586
public BeanDefinition parse(Element element, ParserContext parserContext) {
8687

87-
BeanDefinitionRegistry registry = parserContext.getRegistry();
88+
if (parserContext.isNested()) {
89+
parserContext.getReaderContext().error("Mongo Converter must not be defined as nested bean.", element);
90+
}
8891

92+
BeanDefinitionRegistry registry = parserContext.getRegistry();
8993
String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);
9094
id = StringUtils.hasText(id) ? id : DEFAULT_CONVERTER_BEAN_NAME;
9195

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserIntegrationTests.java

+33-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collections;
2222
import java.util.Set;
2323

24-
import org.junit.Before;
2524
import org.junit.Rule;
2625
import org.junit.Test;
2726
import org.junit.rules.ExpectedException;
@@ -49,25 +48,21 @@
4948
*
5049
* @author Oliver Gierke
5150
* @author Thomas Darimont
51+
* @author Christoph Strobl
5252
*/
5353
public class MappingMongoConverterParserIntegrationTests {
5454

55-
DefaultListableBeanFactory factory;
56-
public @Rule ExpectedException exception = ExpectedException.none();
55+
@Rule public ExpectedException exception = ExpectedException.none();
5756

58-
@Before
59-
public void setUp() {
60-
factory = new DefaultListableBeanFactory();
61-
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
62-
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter.xml"));
63-
}
57+
DefaultListableBeanFactory factory;
6458

6559
/**
6660
* @see DATAMONGO-243
6761
*/
6862
@Test
6963
public void allowsDbFactoryRefAttribute() {
7064

65+
loadValidConfiguration();
7166
factory.getBeanDefinition("converter");
7267
factory.getBean("converter");
7368
}
@@ -78,6 +73,7 @@ public void allowsDbFactoryRefAttribute() {
7873
@Test
7974
public void hasCustomTypeMapper() {
8075

76+
loadValidConfiguration();
8177
MappingMongoConverter converter = factory.getBean("converter", MappingMongoConverter.class);
8278
MongoTypeMapper customMongoTypeMapper = factory.getBean(CustomMongoTypeMapper.class);
8379

@@ -90,6 +86,7 @@ public void hasCustomTypeMapper() {
9086
@Test
9187
public void scansForConverterAndSetsUpCustomConversionsAccordingly() {
9288

89+
loadValidConfiguration();
9390
CustomConversions conversions = factory.getBean(CustomConversions.class);
9491
assertThat(conversions.hasCustomWriteTarget(Person.class), is(true));
9592
assertThat(conversions.hasCustomWriteTarget(Account.class), is(true));
@@ -101,6 +98,7 @@ public void scansForConverterAndSetsUpCustomConversionsAccordingly() {
10198
@Test
10299
public void activatesAbbreviatingPropertiesCorrectly() {
103100

101+
loadValidConfiguration();
104102
BeanDefinition definition = factory.getBeanDefinition("abbreviatingConverter.mongoMappingContext");
105103
Object value = definition.getPropertyValues().getPropertyValue("fieldNamingStrategy").getValue();
106104

@@ -124,6 +122,32 @@ public void rejectsInvalidFieldNamingStrategyConfiguration() {
124122
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-invalid.xml"));
125123
}
126124

125+
/**
126+
* @see DATAMONGO-892
127+
*/
128+
@Test
129+
public void shouldThrowBeanDefinitionParsingExceptionIfConverterDefinedAsNestedBean() {
130+
131+
exception.expect(BeanDefinitionParsingException.class);
132+
exception.expectMessage("Mongo Converter must not be defined as nested bean.");
133+
134+
loadNestedBeanConfiguration();
135+
}
136+
137+
private void loadValidConfiguration() {
138+
this.loadConfiguration("namespace/converter.xml");
139+
}
140+
141+
private void loadNestedBeanConfiguration() {
142+
this.loadConfiguration("namespace/converter-nested-bean-definition.xml");
143+
}
144+
145+
private void loadConfiguration(String configLocation) {
146+
factory = new DefaultListableBeanFactory();
147+
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
148+
reader.loadBeanDefinitions(new ClassPathResource(configLocation));
149+
}
150+
127151
@Component
128152
public static class SampleConverter implements Converter<Person, DBObject> {
129153
public DBObject convert(Person source) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<mongo:db-factory id="factory" />
9+
10+
<bean class="org.springframework.data.mongodb.core.MongoTemplate">
11+
<constructor-arg>
12+
<mongo:mapping-converter />
13+
</constructor-arg>
14+
</bean>
15+
16+
</beans>

0 commit comments

Comments
 (0)