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

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

    Details

    • Type: Bug
    • Status: Open (View Workflow)
    • Priority: Minor
    • Resolution: Unresolved
    • Environment:
    • Similar Issues:

      Description

      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/

        Attachments

          Activity

          There are no comments yet on this issue.

            People

            • Assignee:
              Unassigned
              Reporter:
              loganmzz Logan Mzz
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: