Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect rule validation for non annotated fact parameters #165

Closed
khaledabderrahim opened this issue Jun 11, 2018 · 2 comments
Closed
Labels
Milestone

Comments

@khaledabderrahim
Copy link

khaledabderrahim commented Jun 11, 2018

the message of the exception says :

Condition method  defined in rule must be public, may have parameters annotated with @Fact (and/or exactly one parameter of type or extending Facts)

but in the implementation 👎 just if there is no parameters annotated with @Fact
then the only parameter must be subtype of Facts

@Rule
public class MyRule {
	
	@Action
	public void myAction( @Fact("i") Integer i,Float f ) {
		System.out.println("action method ");
	}
	@Condition
	 public boolean myCondition() {
		return true;
	}
	
	@Priority
	int myRulePriority() {
		return 0;		
	}
}
Method m = myRule.getClass().getDeclaredMethod("myAction", Integer.class,Float.class);
			System.out.println(isActionMethodWellDefined(m));

will print true
solution : change

if(parameterTypes.length == 1 && notAnnotatedParameterCount == 1){
            return Facts.class.isAssignableFrom(parameterTypes[0]);
        }

to :

if(parameterTypes.length == 1 || notAnnotatedParameterCount == 1){
            return Facts.class.isAssignableFrom(parameterTypes[0]);
        }
@fmbenhassine
Copy link
Member

The message of the exception you mentioned is about the condition method, but your example is about the action method. I'm not sure I understand your point.

Can you please clarify? The best way is to provide a test with what you are expecting so I can understand the problem. Thank you upfront.

@fmbenhassine fmbenhassine changed the title RuleDefinitionValidator#validParameters fail consequently RuleDefinitionValidator#checkConditionMethod and checkActionMethods fails too Incorrect error message in condition/action method validation Mar 17, 2019
@fmbenhassine
Copy link
Member

fmbenhassine commented Mar 30, 2019

Hi @khaledabderrahim ,

Thank you for opening this issue! After further analysis, this is indeed a bug in Easy Rules. It is more than the error message being incorrect (and confusing!), it is actually a rule validation issue. The example of rule you gave is invalid (parameter f should either be annotated with @Fact or be of type Facts), but is considered valid. Here is a failing test with 3.2.0:

@Test(expected = IllegalArgumentException.class)
public void methodParametersShouldAllBeAnnotatedWithFactUnlessExactlyOneOfThemIsOfTypeFacts() {

    @Rule
    class MyRule {

        @Action
        public void myAction( @Fact("i") Integer i,Float f ) {
            System.out.println("action method ");
        }
        @Condition
        public boolean myCondition() {
            return true;
        }

        @Priority
        int myRulePriority() {
            return 0;
        }
    }

    new RuleDefinitionValidator().validateRuleDefinition(new MyRule());
}

solution : change

The solution you suggest breaks several tests, so I fixed the issue differently (See df6e0f7). I also updated the error message to be more explicit.

The fix has been deployed in version 3.3.0-SNAPSHOT. Can you give it a try and tell me if this is ok for you? Here is how import the snapshot version if you don't know how to do it.

Kr,
Mahmoud

@fmbenhassine fmbenhassine changed the title Incorrect error message in condition/action method validation Incorrect rule validation for non annotated fact parameters Mar 30, 2019
@fmbenhassine fmbenhassine added this to the v3.3 milestone Mar 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants