Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Cannot add macro parameters programmatically in Confluence version gt 5.2

Ulrich Kuhnhardt [Comalatech]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 16, 2014

From Confluence 5.3 onwards it's not possible any more to add macro parameters programmatically via

try {
            final ContentEntityObject ceo = conversionContext.getEntity();
            String body = xhtmlContent.updateMacroDefinitions(ceo.getBodyAsString(), conversionContext, new MacroDefinitionUpdater(){
                @Override
                public MacroDefinition update(MacroDefinition macroDefinition) {
                    if (macroDefinition.getName().equals("macro-test")){
                        macroDefinition.getParameters().put("visited", new Date().toString());
                    }
                    return macroDefinition;
                }
            });
            ceo.setBodyAsString(body);
            final DefaultSaveContext saveContext = new DefaultSaveContext(true, false, true);
            pageManager.saveContentEntity(ceo, saveContext);

        } catch (XhtmlException e) { ... }

within MyMacro.execute(Map<String, String> stringStringMap, String s, ConversionContext conversionContext)

This problem appears as soon as the macro has a parameter defined like

&lt;xhtml-macro name="macro-test" class="test.TestMacro" key="macroTest"&gt;
        &lt;parameters&gt;
            &lt;parameter name="name" type="string" required="true"/&gt;
....

Even if I add a placeholder in the macro definition for parameter 'visited' it is not added. Works in Confluence 4.3 thru 5.2

4 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Ulrich Kuhnhardt [Comalatech]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 2, 2014

From https://jira.atlassian.com/browse/CONF-32426?focusedCommentId=563880&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-563880

The short answer is the MacroDefinition.parameters map is no longer canonical for what Confluence will save - you need to modify the MacroDefinition.typedParameters map instead, or create a new instance with the updated parameter and value you want to store.

If the macro you're trying to modify was loaded and running last time Confluence saved it, it's been written to storage format as a ac:structured-macro. When this is parsed, Confluence will set the MacroDefinition.storageVersion to 2, populate the MacroDefinition.typedParameters property with the up to date values (using objects where it can parse the parameter into a structured type), and generate the MacroDefinition.parameters string values from these.

When saving a macro, Confluence decides how to save it based on whether it can load its metadata, and it saves its parameters by serializing its typedParameters map. If that map is empty or null, only then will Confluence attempt to save from the values in the parameters map (converting these to match the types in their macro metadata from the xhtml-macro module definition).

As of this date (approaching 5.5), the typeParameters map values can contain Strings, various types of ResourceIdentifiers, or Links containing ResourceIdentifiers. If the parameter isMultiple, then it will contain an Iterator of one of these. The set of types parsed is managed via MacroParameterTypeParserImpl.

I'd like to include structure parameter support for Dates, booleans and integers in the future, but that much wasn't necessary to get content and user rename support working, so was left out of the change that shipped in 5.3. This followup work is not currently scheduled, however, so you're unlikely to see further churn in this area for some months yet.

RonH February 25, 2015

Thank you. This solved my problem.

0 votes
Ulrich Kuhnhardt [Comalatech]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 26, 2014

the code in the repo above shows the problem, J. D. Please have a look at my answer below \/ \/ \/ \/

0 votes
J D
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 25, 2014

Hi @Ulrich Kuhnhardt [Comalatech] , did the code from your repo work for you? Beacause it doesnt work for me. Thanks

0 votes
Ulrich Kuhnhardt [Comalatech]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 16, 2014

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events