Skip to content

Commit 96e3e00

Browse files
authored
Merge pull request #413 from sahvx655-wq/form-extends-constant-order
Fix swapped constant maps in Form.process for extended forms
2 parents 2b8b85b + 6cddb3c commit 96e3e00

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/main/java/org/apache/commons/validator/Form.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected void process(final Map<String, String> globalConstants, final Map<Stri
221221
if (parent != null) {
222222
if (!parent.isProcessed()) {
223223
// we want to go all the way up the tree
224-
parent.process(constants, globalConstants, forms);
224+
parent.process(globalConstants, constants, forms);
225225
}
226226
for (final Field f : parent.getFields()) {
227227
// we want to be able to override any fields we like

src/test/java/org/apache/commons/validator/ExtensionTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
2424

2525
import java.io.InputStream;
26+
import java.util.HashMap;
2627
import java.util.Locale;
28+
import java.util.Map;
2729

2830
import org.junit.jupiter.api.AfterEach;
2931
import org.junit.jupiter.api.BeforeEach;
@@ -108,6 +110,43 @@ void testOrder() {
108110

109111
}
110112

113+
/**
114+
* A form-set constant overrides a global constant of the same name, because {@link Field#process} applies the
115+
* form-set constants before the global ones. This precedence must also hold for a form reached through extension:
116+
* when {@link Form#process} recurses into the parent form it has to pass the global and form-set constants in the
117+
* same order as for a non-extended form. With the arguments swapped the parent's fields resolve a shared constant
118+
* to the global value instead of the form-set value.
119+
*/
120+
@Test
121+
void testConstantOrderWhenExtending() {
122+
final Map<String, String> globalConstants = new HashMap<>();
123+
globalConstants.put("greeting", "global");
124+
final Map<String, String> constants = new HashMap<>();
125+
constants.put("greeting", "formset");
126+
127+
final Field baseField = new Field();
128+
baseField.setProperty("name");
129+
baseField.addVar("msg", "${greeting}", null);
130+
131+
final Form baseForm = new Form();
132+
baseForm.setName("baseForm");
133+
baseForm.addField(baseField);
134+
135+
final Form childForm = new Form();
136+
childForm.setName("childForm");
137+
childForm.setExtends("baseForm");
138+
139+
final Map<String, Form> forms = new HashMap<>();
140+
forms.put("baseForm", baseForm);
141+
forms.put("childForm", childForm);
142+
143+
// Processing the child reaches the still-unprocessed parent through the extension branch of Form.process.
144+
childForm.process(globalConstants, constants, forms);
145+
146+
assertEquals("formset", baseForm.getField("name").getVar("msg").getValue(),
147+
"the form-set constant should override the global constant for an extended form");
148+
}
149+
111150
/**
112151
* Tests if we can override a rule. We "can" override a rule if the message shown when the firstName required test fails and the lastName test is null.
113152
*/

0 commit comments

Comments
 (0)