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

Appending to list doesn't work with some classes

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None
    • Jenkins 2.107.2
      Pipeline 2.5
      Pipeline-groovy 2.49

      It is not possible to add multiple objects to a list and join them to a string.

      Only the first added object will be in the output string.

      This code snippert works perfectly fine in normal groovy, but not with jenkins groovy :-/

      class TemplateParameter implements Serializable {
          final String parameter
          final String value

          TemplateParameter(String parameter, String value) {
              assert parameter
              this.parameter = parameter.toUpperCase()
              assert value
              this.value=value
          }

          String toString() {
              "-p=$parameter=$value"
          }
      }

      node('master') {
          def branch = 'foobar'
          def revision = '1337'
          
          List<TemplateParameter> result = []
          result += new TemplateParameter('rev', revision)
          result += new TemplateParameter('branch', branch)
          println "[DEBUG] Template Parameters size: ${result.size()}"
          println "[DEBUG] Template Parameters: ${result.join(' ')}"
      }

      Output:

      [Pipeline] {
      [Pipeline] echo
      [DEBUG] Template Parameters size: 2
      [Pipeline] echo
      [DEBUG] Template Parameters: -p=REV=1337
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline

      Expected:

      [DEBUG] Template Parameters size: 2
      [DEBUG] Template Parameters: -p=REV=1337 -p=BRANCH=foobar

       

       

          [JENKINS-51083] Appending to list doesn't work with some classes

          Andrew Bayer added a comment -

          I'll check when I get a chance to see why += isn't working, but, fwiw, someList << someObject or someList.add(someObject) will work fine.

          Andrew Bayer added a comment - I'll check when I get a chance to see why += isn't working, but, fwiw, someList << someObject or someList.add(someObject) will work fine.

          Actually i can reproduce the problem with << and .add().

          The size of the list is 2 in my example, so the object gets added.

          If i use a String in instead of the class TemplateParameter then this problem does not occur.

          I think the problem is maybe not related to the addition of another object.

           

          Michael Werner added a comment - Actually i can reproduce the problem with << and .add(). The size of the list is 2 in my example, so the object gets added. If i use a String in instead of the class TemplateParameter then this problem does not occur. I think the problem is maybe not related to the addition of another object.  

            Unassigned Unassigned
            xaseron Michael Werner
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: