Permalink
Browse files

Fix code related to making command objects validateble. Fixes #10091

  • Loading branch information...
1 parent 69fca8c commit ce049fd571fb259331c1afc66c2011e31d37f2b8 @graemerocher graemerocher committed Nov 8, 2016
@@ -157,9 +157,16 @@ protected Class getAnnotationTypeClass() {
}
public void performInjectionOnArtefactType(SourceUnit sourceUnit, ClassNode cNode, String artefactType) {
- doPerformInjectionOnArtefactType(sourceUnit, cNode, artefactType);
+ List<ClassInjector> injectors = findInjectors(artefactType, GrailsAwareInjectionOperation.getClassInjectors());
+ for (ClassInjector injector : injectors) {
+ if(injector instanceof CompilationUnitAware) {
+ ((CompilationUnitAware) injector).setCompilationUnit(this.compilationUnit);
+ }
+ }
+ performInjection(sourceUnit, cNode, injectors);
}
+ @Deprecated
public static void doPerformInjectionOnArtefactType(SourceUnit sourceUnit, ClassNode cNode, String artefactType) {
List<ClassInjector> injectors = findInjectors(artefactType, GrailsAwareInjectionOperation.getClassInjectors());
performInjection(sourceUnit, cNode, injectors);
@@ -736,18 +736,23 @@ protected void initializeAndValidateCommandObjectParameter(final BlockStatement
javax.persistence.Entity.class) ||
commandObjectNode.implementsInterface(ClassHelper.make(Validateable.class));
- if (!argumentIsValidateable) {
+ if (!argumentIsValidateable && commandObjectNode.isPrimaryClassNode()) {
final ModuleNode commandObjectModule = commandObjectNode.getModule();
- if (commandObjectModule != null) {
+ if (commandObjectModule != null && this.compilationUnit != null) {
if (commandObjectModule == controllerNode.getModule() ||
doesModulePathIncludeSubstring(commandObjectModule,
"grails-app" + File.separator + "controllers" + File.separator)) {
TraitInjectionUtils.injectTrait(compilationUnit, source, commandObjectNode, Validateable.class);
-/* ConstructorNode constructorNode = commandObjectNode.getDeclaredConstructors().get(0);
- ClassNode helper = Traits.findHelper(ClassHelper.make(Validateable.class));
- ((BlockStatement)constructorNode.getCode()).addStatement(new ExpressionStatement(new StaticMethodCallExpression(helper, "$init$", VariableExpression.THIS_EXPRESSION)));*/
+ List<ConstructorNode> declaredConstructors = commandObjectNode.getDeclaredConstructors();
+ if(declaredConstructors.isEmpty()) {
+ BlockStatement constructorLogic = new BlockStatement();
+ ConstructorNode constructorNode = new ConstructorNode(Modifier.PUBLIC, constructorLogic);
+ ClassNode helper = Traits.findHelper(ClassHelper.make(Validateable.class));
+ commandObjectNode.addConstructor(constructorNode);
+ constructorLogic.addStatement(new ExpressionStatement(new StaticMethodCallExpression(helper, "$init$", VariableExpression.THIS_EXPRESSION)));
+ }
argumentIsValidateable = true;
} else if (doesModulePathIncludeSubstring(commandObjectModule,
"grails-app" + File.separator + "domain" + File.separator)) {
@@ -38,26 +38,32 @@ trait Validateable {
private static Map<String, ConstrainedProperty> constraintsMapInternal
Errors errors
+ /**
+ * @return The errors
+ */
Errors getErrors() {
initErrors()
errors
}
+ /**
+ * @return Whether the object has errors
+ */
Boolean hasErrors() {
initErrors()
errors.hasErrors()
}
+ /**
+ * Clear the errors
+ */
void clearErrors() {
errors = null
}
- private void initErrors() {
- if (errors == null) {
- errors = new ValidationErrors(this, this.getClass().getName())
- }
- }
-
+ /**
+ * @return The map of applied constraints
+ */
static Map<String, ConstrainedProperty> getConstraintsMap() {
if (constraintsMapInternal == null) {
ConstraintsEvaluator evaluator = findConstraintsEvaluator()
@@ -67,45 +73,74 @@ trait Validateable {
constraintsMapInternal
}
- @CompileStatic
- private static ConstraintsEvaluator findConstraintsEvaluator() {
- try {
- BeanFactory ctx = Holders.applicationContext
- ConstraintsEvaluator evaluator = ctx.getBean(ConstraintsEvaluator.BEAN_NAME, ConstraintsEvaluator)
- return evaluator
- } catch (Throwable e) {
- return new DefaultConstraintEvaluator()
- }
- }
-
+ /**
+ * Validate the object
+ *
+ * @return True if it is valid
+ */
boolean validate() {
validate null, null, null
}
+ /**
+ * Validate the object with the given adhoc constraints
+ *
+ * @return True if it is valid
+ */
boolean validate(Closure<?>... adHocConstraintsClosures) {
validate(null, null, adHocConstraintsClosures)
}
+ /**
+ * Validate the object with the given parameters
+ *
+ * @return True if it is valid
+ */
boolean validate(Map<String, Object> params) {
validate params, null
}
+ /**
+ * Validate the object with the given parameters and adhoc constraints
+ *
+ * @return True if it is valid
+ */
boolean validate(Map<String, Object> params, Closure<?>... adHocConstraintsClosures) {
validate(null, params, adHocConstraintsClosures)
}
+ /**
+ * Validate the object for the given list of fields
+ *
+ * @return True if it is valid
+ */
boolean validate(List fieldsToValidate) {
validate fieldsToValidate, null, null
}
+ /**
+ * Validate the object for the given list of fields and adhoc constraints
+ *
+ * @return True if it is valid
+ */
boolean validate(List fieldsToValidate, Closure<?>... adHocConstraintsClosures) {
validate(fieldsToValidate, null, adHocConstraintsClosures)
}
+ /**
+ * Validate the object for the given list of fields and parameters
+ *
+ * @return True if it is valid
+ */
boolean validate(List fieldsToValidate, Map<String, Object> params) {
validate fieldsToValidate, params, null
}
+ /**
+ * Validate the object for the given list of fields, parameters and adhoc constraints
+ *
+ * @return True if it is valid
+ */
boolean validate(List fieldsToValidate, Map<String, Object> params, Closure<?>... adHocConstraintsClosures) {
beforeValidateHelper.invokeBeforeValidate(this, fieldsToValidate)
@@ -157,6 +192,17 @@ trait Validateable {
this.getProperty(prop.propertyName)
}
+ @CompileStatic
+ private static ConstraintsEvaluator findConstraintsEvaluator() {
+ try {
+ BeanFactory ctx = Holders.applicationContext
+ ConstraintsEvaluator evaluator = ctx.getBean(ConstraintsEvaluator.BEAN_NAME, ConstraintsEvaluator)
+ return evaluator
+ } catch (Throwable e) {
+ return new DefaultConstraintEvaluator()
+ }
+ }
+
private MessageSource findMessageSource() {
try {
ApplicationContext ctx = Holders.applicationContext
@@ -167,6 +213,12 @@ trait Validateable {
}
}
+ private void initErrors() {
+ if (errors == null) {
+ errors = new ValidationErrors(this, this.getClass().getName())
+ }
+ }
+
static boolean defaultNullable() {
false
}
@@ -191,7 +191,7 @@ class CommandObjectsSpec extends Specification {
when:
def model = controller.methodAction()
def person = model.person
- def constrainedProperties = person.constraints
+ def constrainedProperties = person.constraintsMap
def nameConstrainedProperty = constrainedProperties.name
def matchesProperty = nameConstrainedProperty.matches
@@ -1,5 +1,6 @@
package org.grails.web.taglib
+import grails.util.Environment
import org.grails.taglib.GrailsTagException
/**
@@ -10,6 +11,11 @@ import org.grails.taglib.GrailsTagException
*/
class CoreTagsTests extends AbstractGrailsTagTests {
+ @Override
+ protected void onSetUp() {
+ System.setProperty(Environment.KEY, "")
+ }
+
void testUnlessWithTestCondition() {
def template = '<g:unless test="${cond}">body text</g:unless>'
assertOutputEquals 'body text', template, [cond: false], {it.toString().trim()}

0 comments on commit ce049fd

Please sign in to comment.