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

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      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
      

       

            Assignee:
            Andrew Bayer
            Reporter:
            Sebastian Flügge
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Resolved:
              Archived: