Workaround for empty parameters Jenkins bug

If you tried to set up a Jenkins build using Jenkins-DSL plugin, you may have hit a bug (JENKINS-33201) where Jenkins refuses to build your code because parameters are there - but they are empty.

The actual bug may be caused by one of its plugins, not the core Jenkins itself. But it is not nice that the DSL plugin generates build parameters node even if there is nothing to put in it.

The symptom is this exception when you are trying to run it:

java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to net.sf.json.JSONObject
  at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinitionProperty.java:143)

In order to fix it, use the body of the following snippet:

job {
  configure {
    Node params = it / 'properties' / 'hudson.model.ParametersDefinitionProperty'
    if (params) {
      (it / 'properties').remove(params)
    }
}

Of course you can improve it with a check to see if it is empty and place it in a helper method. That is if you have more than one job to suffer from the bug.