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

hasProperty returns null on pipeline context (e.g. for Job Parameters)

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      hasProperty does not work as expected when used on the this-context of a pipeline script. It should return true for existing parameter and false for non-existing parameter but it returns null in both cases. getProperty in contrast works as expected.

      Steps to reproduce:

       Create parameterized Pipeline job with following parameters:

      • string parameter myProperty and value "present" and the following pipeline script
      • // Pipeline Script
        pipeline {
            agent none
            stages {
                stage('HasPropertyTest') {
                    steps {
                        script {
                            echo "Value of 'myProperty': ${getProperty("myProperty")}"
                            echo "Got: ${hasProperty("myProperty")} - expected ${true}"
                            try {
                                echo "Value of 'missingProperty' ${getProperty("missingProperty")}"
                            } catch (MissingPropertyException) {
                                echo "Parameter 'missingProperty' does not exist."
                            }
                            echo "Got: ${hasProperty("missingProperty")} - expected ${false}"
                            if(hasProperty("myProperty")) {
                                // success
                            } else {
                                error "No Property: myProperty"
                            }
                        }
                    }
                }
            }
        }
        
        
      • No Groovy Sandbox

      Got Output:

      Running in Durability level: MAX_SURVIVABILITY
      [Pipeline] stage
      [Pipeline] { (HasPropertyTest)
      [Pipeline] script
      [Pipeline] {
      [Pipeline] echo
      Value of 'myProperty': present
      [Pipeline] echo
      Got: null - expected true
      [Pipeline] echo
      Parameter 'missingProperty' does not exist.
      [Pipeline] echo
      Got: null - expected false
      [Pipeline] error
      [Pipeline] }
      [Pipeline] // script
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] End of Pipeline
      ERROR: No Property: myProperty
      Finished: FAILURE
      

      Expected Output:

      Running in Durability level: MAX_SURVIVABILITY
      [Pipeline] stage
      [Pipeline] { (HasPropertyTest)
      [Pipeline] script
      [Pipeline] {
      [Pipeline] echo
      Value of 'myProperty': present
      [Pipeline] echo
      Got: true - expected true
      [Pipeline] echo
      Parameter 'missingProperty' does not exist.
      [Pipeline] echo
      Got: false - expected false
      [Pipeline] }
      [Pipeline] // script
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

       

        Attachments

          Activity

          Hide
          abayer Andrew Bayer added a comment -

          So hasProperty is actually http://docs.groovy-lang.org/2.4.3/html/groovy-jdk/java/lang/Object.html#hasProperty(java.lang.String) - which isn't actually what you're looking for. The behavior you're seeing is what I'd expect. This is a bit confusing because getProperty is actually CpsScript#getProperty(String), overriding Script#getProperty(String). Script#getProperty(String) checks in the Script#binding for a variable of the given name, and CpsScript#getProperty(String) tries that, falling back to look for a GlobalVariable or an environment variable.

          Show
          abayer Andrew Bayer added a comment - So hasProperty is actually http://docs.groovy-lang.org/2.4.3/html/groovy-jdk/java/lang/Object.html#hasProperty(java.lang.String ) - which isn't actually what you're looking for. The behavior you're seeing is what I'd expect. This is a bit confusing because getProperty is actually CpsScript#getProperty(String) , overriding Script#getProperty(String) . Script#getProperty(String) checks in the Script#binding for a variable of the given name, and CpsScript#getProperty(String) tries that, falling back to look for a GlobalVariable or an environment variable.
          Hide
          jmordkoff Jeremy Mordkoff added a comment -

          Is there a safe way to determine if a pipeline returned by 'return this' and loaded by 'load scriptfile' has defined a method or parameter? 

          Show
          jmordkoff Jeremy Mordkoff added a comment - Is there a safe way to determine if a pipeline returned by 'return this' and loaded by 'load scriptfile' has defined a method or parameter? 
          Hide
          abayer Andrew Bayer added a comment -

          Jeremy Mordkoff All I can think of is just trying to run the method in question. =)

          Show
          abayer Andrew Bayer added a comment - Jeremy Mordkoff All I can think of is just trying to run the method in question. =)
          Hide
          abayer Andrew Bayer added a comment -

          Closing this as not a bug, since it wasn't. =)

          Show
          abayer Andrew Bayer added a comment - Closing this as not a bug, since it wasn't. =)

            People

            Assignee:
            abayer Andrew Bayer
            Reporter:
            seflue Sebastian Flügge
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: