When evaluating a switch statement, if a case branch is selected, regardless of whether or not "fallthrough" is true, the default case branch, if present, will be selected as well, resulting in the selected case and the default case being evaluated when only the selected case should be evaluated.

Let's say you have a string and it's value is "foo". When switching on this string, we have two cases and one default case:

case "foo"
case "bar"
default case

The current behavior results in both case "foo" and the default case both evaulating, when we would expect only case "foo" to execute. Fallthrough is false on both case "foo" and case "bar".

Comments

heddn’s picture

Status: Active » Needs review
StatusFileSize
new960 bytes

This seems to resolve the issue.

micromegas’s picture

Assigned: Unassigned » micromegas
Issue summary: View changes
StatusFileSize
new1.78 KB

Patch from #1 caused the default case to always get removed, even if the previous case was set to fall through.

New patch overrides canEvaluate() in RulesConditionalDefaultCase to only return TRUE if there are no other valid siblings, or if the previous valid sibling was set to fall through--these are the only two conditions that we would want the default case to apply.

Anonymous’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Assigned: micromegas » Unassigned
Status: Needs review » Needs work

The patch from #2 didn't work for me... One of my conditions (the one directly above the default case) wasn't evaluated (or if it was it was then overridden by the default). 'Fall through' wasn't set anywhere.

micromegas’s picture

Status: Needs work » Needs review
StatusFileSize
new1.78 KB

Fixed typos in getAllSibling()

micromegas’s picture

Did anyone have a chance to test patch #3? I've been using it in production for about 3 months without any issues.

kliban’s picture

I encountered the "default" problem today. Patch #3 solved it beautifully, on a very simple switch though (one case, one default). I'll test it further on as soon as I have the opportunity. Thx for the great job.

rodpal’s picture

Patch #3 is working perfectly. Thanks @micromegas!

micromegas’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for testing; I also have been using this heavily for about 9 months with no issues. Setting to RTBC.

cattmote’s picture

I can confirm that Patch #3 resolved the issue for me as well. Thanks for the contribution @micromegas :)

johnorourke’s picture

Thanks @micromegas , worked perfectly for me too

granticusiv’s picture

It looks like both 7.x-1.0-beta2 and 7.x-1.x-dev have 'function canEvaluate(RulesState $state)' declaration, so I adapted the patch and it worked for me on 7.x-1.0-beta2

Edit: Silly me :S I was looking in the wrong Class/file. I've applied the patch to the correct Classes in 7.x-1.0-beta2 and it's breaking when a Case is evaluated TRUE, rather than always returning the Default case.

alansaviolobo’s picture

+1 RTBC

heddn’s picture

To be clear here, we want patch from #4 merged? There were some comments about #3 too.

alansaviolobo’s picture

I believe the confusion is because the patch number 3 is actually uploaded in comment #4.
But yes, that was the one I tested.

JMTorres’s picture

+1 Works for me in production for over a year.

lelivero’s picture

#4 works perfect for me in a "multiple cases" situation.
Conditional Rules 7.x-1.0-beta2+2-dev (2013-okt-01) and Rules 7.x-2.10

  • alansaviolobo committed 0712b47 on 7.x-1.x
    Issue #2038193 by micromegas: Switch Default Case Improper Behavior
    
alansaviolobo’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.