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

Inconsistent return value handling for GStrings

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None

      I am currently writing automated tests for Jenkins pipelines and corresponding utility functions. Doing so, I switched some code which previously used string concatenation to now use GStrings to improve readability.

      My tests (written in Groovy with `JenkinsPipelineUnit`) did not show any issues/differences and the Script Console seems to correctly interpret this as well, but as soon as running the code in the pipelines itself, I am observing issues which would write the internal representation of GStrings to my JSON file.

      Take the following code for example, which contains a standalone re-implementation of https://github.com/jenkinsci/pipeline-utility-steps-plugin/blob/master/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/json/WriteJSONStep.java for testing purposes:

      import net.sf.json.JSON;
      import net.sf.json.JSONSerializer;
      
      job = 42
      
      String writeJSON(Map data) {
        StringWriter writer = new StringWriter();
        JSON jsonObject = JSONSerializer.toJSON(data);
        jsonObject.write(writer);
        return writer.toString();
      }
      
      String get1() {
        def result = "abc/${job}"
        println 'get1.result: ' + result.getClass()
        return result
      }
      
      org.codehaus.groovy.runtime.GStringImpl get2() {
        def result = "abc/${job}"
        println 'get2.result: ' + result.getClass()
        return result
      }
      
      def result1 = get1()
      println 'get1.return: ' + result1.getClass()
      println writeJSON([key: result1])
      
      def result2 = get2()
      println 'get2.return: ' + result2.getClass()
      println writeJSON([key: result2])
      

      This will print the following output:

      get1.result: class org.codehaus.groovy.runtime.GStringImpl
      get1.return: class java.lang.String
      {"key":"abc/42"}
      get2.result: class org.codehaus.groovy.runtime.GStringImpl
      get2.return: class org.codehaus.groovy.runtime.GStringImpl
      {"key":{"valueCount":1,"strings":["abc/",""],"bytes":[97,98,99,47,52,50],"values":[42]}}
      

      As we can see, while in `get1` the `result` variable is a GString, it automatically gets converted to a string due to the return type of the method. When running this from within the pipeline itself, the automatic conversion does not seem to be take place, which is confusing and apparently cannot be tested from the "outside".

            rsandell rsandell
            stefan6419846 Stefan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: