Tuesday, July 22, 2014

Jenkins/Stapler: @DataBoundConstructor being ignored, parameter values not passed or null

If you're developing a plugin or patch for Jenkins, which uses the Stapler framework, you might run into issues where you define a new @DataBoundConstructor with an additional parameter, but it just seems to be ignored by the framework.

If so, look for an overridden newInstance method. It's probably being used instead of the constructor annotation.



For example, I was adding a new parameter to Shell.java, so:

    @DataBoundConstructor
    public Shell(String command) {
        super(fixCrLf(command));
    }

became

    @DataBoundConstructor
    public Shell(String command, Integer unstableReturn) {
        super(fixCrLf(command));
        this.unstableReturn = unstableReturn;
    }

    public Shell(String command) {
        this(command, null);
    }

    private final Integer unstableReturn;

    public final Integer getUnstableReturn() {
        return unstableReturn;
    }

with the associated extra UI element in config.groovy:

    f.entry(title:_("Return code to set build unstable"), description:_("If set, the script return code that will be interpreted as an unstable build result.")) {
        f.number(name: "unstableReturn", value: instance?.unstableReturn)
    }

... but it was always null.

The cause was that the old ctor was always being called because of the inner class:

    @Extension
    public static class DescriptorImpl extends BuildStepDescriptor {

        //....

        @Override
        public Builder newInstance(StaplerRequest req, JSONObject data) {
            return new Shell(data.getString("command"));
        }

        //....

     }

so there you go, if your UI element just seems to be ignored, check if this is an issue.

No comments:

Post a Comment

Captchas suck. Bots suck more. Sorry.