Skip to content

Apache Lucene open-source search software

SOLR-5227: Correctly fail schema init if a dynamicField is configured to be required, or have a default value

Chris M. Hostetter88a00de5b7fc
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 7e67ce331c7..d1d9bfc2843 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -76,6 +76,14 @@ Upgrading from Solr 4.4.0
XXXXX and delete the ones that you do not wish to use. See SOLR-4953 &
SOLR-5108 for more details.
+* In the past, schema.xml parsing would silently ignore "default" or "required"
+ options specified on <dynamicField/> declarations. Begining with 4.5, attempting
+ to do configured these on a dynamic field will cause an init error. If you
+ encounter one of these errors when upgrading an existing schema.xml, you can
+ safely remove these attributes, regardless of their value, from your config and
+ Solr will continue to bahave exactly as it did in previous versions. See
+ SOLR-5227 for more details.
+
* The UniqFieldsUpdateProcessorFactory has been improved to support all of the
FieldMutatingUpdateProcessorFactory selector options. The <lst named="fields">
init param option is now deprecated and should be replaced with the more standard
@@ -201,6 +209,9 @@ Bug Fixes
* SOLR-4909: Use DirectoryReader.openIfChanged in non-NRT mode.
(Michael Garski via Robert Muir)
+* SOLR-5227: Correctly fail schema initalization if a dynamicField is configured to
+ be required, or have a default value. (hossman)
+
Optimizations
----------------------
diff --git a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
index 51ec4b94a14..2142a8b3539 100644
--- a/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
+++ b/solr/core/src/java/org/apache/solr/schema/IndexSchema.java
@@ -674,6 +674,14 @@ public class IndexSchema {
requiredFields.add(f);
}
} else if (node.getNodeName().equals(DYNAMIC_FIELD)) {
+ if( f.getDefaultValue() != null ) {
+ throw new SolrException(ErrorCode.SERVER_ERROR,
+ DYNAMIC_FIELD + " can not have a default value: " + name);
+ }
+ if ( f.isRequired() ) {
+ throw new SolrException(ErrorCode.SERVER_ERROR,
+ DYNAMIC_FIELD + " can not be required: " + name);
+ }
if (isValidFieldGlob(name)) {
// make sure nothing else has the same path
addDynamicField(dFields, f);
diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-dynamicfield-default-val.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-dynamicfield-default-val.xml
new file mode 100644
index 00000000000..0e3595d75cb
--- /dev/null
+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-dynamicfield-default-val.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<schema name="bad-schema-dynamicfield-default-val" version="1.4">
+ <types>
+ <fieldType name="string" class="solr.StrField"/>
+ </types>
+
+ <fields>
+ <field name="id" type="string" indexed="true" stored="true" />
+ <!-- BEGIN BAD STUFF -->
+ <dynamicField name="bad_*" type="string" default="BAD" />
+ <!-- END BAD STUFF -->
+ </fields>
+
+ <defaultSearchField>id</defaultSearchField>
+ <uniqueKey>id</uniqueKey>
+
+</schema>
diff --git a/solr/core/src/test-files/solr/collection1/conf/bad-schema-dynamicfield-required.xml b/solr/core/src/test-files/solr/collection1/conf/bad-schema-dynamicfield-required.xml
new file mode 100644
index 00000000000..c372afd44a4
--- /dev/null
+++ b/solr/core/src/test-files/solr/collection1/conf/bad-schema-dynamicfield-required.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" ?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<schema name="bad-schema-dynamicfield-required" version="1.4">
+ <types>
+ <fieldType name="string" class="solr.StrField"/>
+ </types>
+
+ <fields>
+ <field name="id" type="string" indexed="true" stored="true" />
+ <!-- BEGIN BAD STUFF -->
+ <dynamicField name="bad_*" type="string" required="true" />
+ <!-- END BAD STUFF -->
+ </fields>
+
+ <defaultSearchField>id</defaultSearchField>
+ <uniqueKey>id</uniqueKey>
+
+</schema>
diff --git a/solr/core/src/test-files/solr/collection1/conf/schema_codec.xml b/solr/core/src/test-files/solr/collection1/conf/schema_codec.xml
index 73593829cf9..4e49dce953e 100644
--- a/solr/core/src/test-files/solr/collection1/conf/schema_codec.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/schema_codec.xml
@@ -41,8 +41,8 @@
<dynamicField name="*_pulsing" type="string_pulsing" indexed="true" stored="true"/>
<dynamicField name="*_standard" type="string_standard" indexed="true" stored="true"/>
- <dynamicField name="*_disk" type="string_disk" indexed="false" stored="false" docValues="true" default="" />
- <dynamicField name="*_memory" type="string_memory" indexed="false" stored="false" docValues="true" default="" />
+ <dynamicField name="*_disk" type="string_disk" indexed="false" stored="false" docValues="true" />
+ <dynamicField name="*_memory" type="string_memory" indexed="false" stored="false" docValues="true" />
</fields>
<defaultSearchField>string_f</defaultSearchField>
<uniqueKey>string_f</uniqueKey>
diff --git a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
index a4453ddfbc3..d5d67e69259 100644
--- a/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/BadIndexSchemaTest.java
@@ -40,6 +40,10 @@ public class BadIndexSchemaTest extends AbstractBadConfigTestBase {
public void testSevereErrorsForDuplicateDynamicField() throws Exception {
doTest("bad-schema-dup-dynamicField.xml", "_twice");
}
+ public void testSevereErrorsForUnsupportedAttributesOnDynamicField() throws Exception {
+ doTest("bad-schema-dynamicfield-default-val.xml", "default");
+ doTest("bad-schema-dynamicfield-required.xml", "required");
+ }
public void testSevereErrorsForDuplicateFieldType() throws Exception {
doTest("bad-schema-dup-fieldType.xml", "ftAgain");