Search code examples
javajenkinsjenkins-pluginsjelly

Jenkins-Plugin: Optional block and its nested radio buttons are not persisted after saving and loading the configuration dialogue


I'm trying to get started with Jenkins plugin development and have a little problem regarding the persistence of a global configuration of the plugin.

The option in question is an optional block with 2 nested radio buttons, as you can see in the plugin's global.jelly:

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

  <f:section title="Hello World Builder">
    <f:optionalBlock name="useForeignLanguage" title="Use foreign language" checked="useForeignLanguage">
      <f:entry title="Language to use" field="useLanguage"
        description="Check if we should say hello in another language">
        <f:radio name="useLanguage" title="French" value="fr" />
        <f:radio name="useLanguage" title="Japanese" value="jp" />
      </f:entry>
    </f:optionalBlock>
  </f:section>

</j:jelly>

The relevant Java code is as follows:

public class HelloWorldBuilder extends Builder {
    //...
    @Extension
    public static final class Descriptor extends BuildStepDescriptor<Builder> {
        private boolean useForeignLanguage;
        private String  useLanguage;

        public Descriptor() {
            load();
        }

        @Override
        public boolean configure( final StaplerRequest req, final JSONObject formData ) throws FormException {
            // To persist global configuration information,
            // set that to properties and call save().
            final JSONObject languageJSON = formData.getJSONObject( "useForeignLanguage" );
            if ( ( languageJSON != null ) && !( languageJSON.isNullObject() ) ) {
                this.useLanguage = languageJSON.getString( "useLanguage" );
                this.useForeignLanguage = true;
            } else {
                this.useLanguage = null;
                this.useForeignLanguage = false;
            }
            save();
            return super.configure( req, formData );
        }

        public String getUseLanguage() {
            return this.useLanguage;
        }

        public boolean getUseForeignLanguage() {
            return this.useForeignLanguage;
        }

        //...
    }
}

The config itself gets saved fine in an xml, which is loaded everytime the configuration is saved, so I guess the problem lies within loading and filling the form modules:

<?xml version='1.0' encoding='UTF-8'?>
<bar.foo.testing.HelloWorldBuilder_-Descriptor plugin="YetAnotherPluginTest@1.0-SNAPSHOT">
  <useForeignLanguage>true</useForeignLanguage>
  <useLanguage>jp</useLanguage>
</bar.foo.testing.HelloWorldBuilder_-Descriptor>

Thanks for any help in advance


Solution

  • Updating the jelly fixed the problem:

    <j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson"
      xmlns:f="/lib/form">
    
      <f:section title="Hello World Builder">
        <f:optionalBlock name="useForeignLanguage" title="Use foreign language" checked="${instance.useForeignLanguage}">
          <f:entry title="Language to use" field="useLanguage" description="Check if we should say hello in another language">
              <f:entry>
                <f:radio name="useLanguage" title="French" value="fr" checked="${instance.useLanguage == 'fr'}" />
              </f:entry>
              <f:entry>
                <f:radio name="useLanguage" title="Japanese" value="jp" checked="${instance.useLanguage == 'jp'}" />
              </f:entry>
          </f:entry>
        </f:optionalBlock>
      </f:section>
    
    </j:jelly>