Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-54652

[Groovy] Invalid switch case flow on empty "block"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major

      In standard C-based language (such as Java, Groovy, ...), switch cases ends with break keyword. When there's no statement that break the flow, the next case is executed. For example:

      String value = "foo";
      switch (value) {
          case "rab":
              System.out.println("rab");
          case "oof":
              System.out.println("oof");
              break;
      
          case "foo":
              System.out.println("foo");
          case "bar":
              System.out.println("bar");
              break;
      }
      

      It must print "foobar".

      However, following pipeline prints: 1, null and 2.

      def switchCase(value) {
      	switch (value) {
      		case 'foo':
      			return 1
      		case 'bar':
      		default:
      			return 2
      	}
      }
      
      pipeline {
      	agent any
      
      	options {
      		buildDiscarder(logRotator(numToKeepStr:'5'))
      		disableConcurrentBuilds()
      		timestamps()
      		skipDefaultCheckout()
      	}
      
      	stages {
      		stage('Switch') {
      			steps {
      				echo "foo  -> '${switchCase('foo')}'"
      				echo "bar  -> '${switchCase('bar')}'"
      				echo "null -> '${switchCase(null)}'"
      			}
      		}
      	}
      }
      

      You can check its valid on Groovy through: https://groovyconsole.appspot.com/

            Unassigned Unassigned
            loganmzz Logan Mzz
            Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: