Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/validator/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ protected void process(final Map<String, String> globalConstants, final Map<Stri
if (parent != null) {
if (!parent.isProcessed()) {
// we want to go all the way up the tree
parent.process(constants, globalConstants, forms);
parent.process(globalConstants, constants, forms);
}
for (final Field f : parent.getFields()) {
// we want to be able to override any fields we like
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/apache/commons/validator/ExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -108,6 +110,43 @@ void testOrder() {

}

/**
* A form-set constant overrides a global constant of the same name, because {@link Field#process} applies the
* form-set constants before the global ones. This precedence must also hold for a form reached through extension:
* when {@link Form#process} recurses into the parent form it has to pass the global and form-set constants in the
* same order as for a non-extended form. With the arguments swapped the parent's fields resolve a shared constant
* to the global value instead of the form-set value.
*/
@Test
void testConstantOrderWhenExtending() {
final Map<String, String> globalConstants = new HashMap<>();
globalConstants.put("greeting", "global");
final Map<String, String> constants = new HashMap<>();
constants.put("greeting", "formset");

final Field baseField = new Field();
baseField.setProperty("name");
baseField.addVar("msg", "${greeting}", null);

final Form baseForm = new Form();
baseForm.setName("baseForm");
baseForm.addField(baseField);

final Form childForm = new Form();
childForm.setName("childForm");
childForm.setExtends("baseForm");

final Map<String, Form> forms = new HashMap<>();
forms.put("baseForm", baseForm);
forms.put("childForm", childForm);

// Processing the child reaches the still-unprocessed parent through the extension branch of Form.process.
childForm.process(globalConstants, constants, forms);

assertEquals("formset", baseForm.getField("name").getVar("msg").getValue(),
"the form-set constant should override the global constant for an extended form");
}

/**
* 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.
*/
Expand Down
Loading