Skip to content

Commit 8153207

Browse files
committed
DATA-1293 - Rather than list disallowed attributes - calculate count of allowed attributes and compare it to total attributes count.
1 parent 02e3387 commit 8153207

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import static org.springframework.data.config.ParsingUtils.*;
1919
import static org.springframework.data.mongodb.config.MongoParsingUtils.*;
2020

21+
import java.util.Collections;
22+
import java.util.HashSet;
23+
import java.util.Set;
24+
2125
import org.springframework.beans.factory.BeanDefinitionStoreException;
2226
import org.springframework.beans.factory.config.BeanDefinition;
2327
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
@@ -47,6 +51,15 @@
4751
* @author Viktor Khoroshko
4852
*/
4953
public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
54+
private static final Set<String> MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES;
55+
56+
static {
57+
Set<String> mongoUriAllowedAdditionalAttributes = new HashSet<String>();
58+
mongoUriAllowedAdditionalAttributes.add("id");
59+
mongoUriAllowedAdditionalAttributes.add("write-concern");
60+
61+
MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES = Collections.unmodifiableSet(mongoUriAllowedAdditionalAttributes);
62+
}
5063

5164
/*
5265
* (non-Javadoc)
@@ -74,10 +87,14 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
7487
BeanDefinition mongoUri = getMongoUri(element);
7588

7689
if (mongoUri != null) {
77-
if (element.hasAttribute("mongo-ref") || element.hasAttribute("dbname")
78-
|| element.hasAttribute("authentication-dbname")
79-
|| element.hasAttribute("port") || element.hasAttribute("host")
80-
|| element.hasAttribute("username") || element.hasAttribute("password")) {
90+
int allowedAttributesCount = 1;
91+
for (String attribute : MONGO_URI_ALLOWED_ADDITIONAL_ATTRIBUTES) {
92+
if (element.hasAttribute(attribute)) {
93+
allowedAttributesCount++;
94+
}
95+
}
96+
97+
if (element.getAttributes().getLength() > allowedAttributesCount) {
8198
parserContext.getReaderContext().error("Configure either Mongo URI or details individually!",
8299
parserContext.extractSource(element));
83100
}

0 commit comments

Comments
 (0)