Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'UnsupportedOperationException' when passing immutable Map as options to 'createPhraseNode' #1221

Closed
abelsromero opened this issue Jun 17, 2023 · 1 comment · Fixed by #1222
Labels

Comments

@abelsromero
Copy link
Member

Passing a map created with Map.of as in example below causes error:

java.lang.UnsupportedOperationException
	at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
	at java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:1072)
	at org.asciidoctor.jruby.extension.internal.JRubyProcessor.createPhraseNode(JRubyProcessor.java:200)
	at org.asciidoctor.extension.BaseProcessor.createPhraseNode(BaseProcessor.java:109)
	at org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor.process(ManpageInlineMacroProcessor.java:20)
	at org.asciidoctor.maven.test.processors.ManpageInlineMacroProcessor.process(ManpageInlineMacroProcessor.java:8)
	at org.asciidoctor.jruby.extension.processorproxies.InlineMacroProcessorProxy.process(InlineMacroProcessorProxy.java:123)
public class ManpageInlineMacroProcessor extends InlineMacroProcessor {

    public ManpageInlineMacroProcessor(String macroName) {
        super(macroName);
    }

    @Override
    public String process(ContentNode parent, String target, Map<String, Object> attributes) {

        final Map<String, Object> options = Map.of(
                "type", ":link",
                "target", target + ".html");
        return createPhraseNode(parent, "anchor", target, attributes, options).convert();
    }

}

I see the code modifies the user input map

options.put(Options.ATTRIBUTES, RubyHashUtil.convertMapToRubyHashWithStrings(rubyRuntime, attributes));
. I don't think we should be modifying user input but creating a copy for:

  • Safety: the user may be doing something else with the Map.
  • Java support: immutable constructors should be supported, I don't see a reason why we should not.

There might be other similar cases with other user input 🤔

@robertpanzer
Copy link
Member

Agree, we should create a copy of the map.

robertpanzer added a commit to robertpanzer/asciidoctorj that referenced this issue Jun 18, 2023
Create mutable copies of options passed to Processor.create...()
robertpanzer added a commit to robertpanzer/asciidoctorj that referenced this issue Jun 18, 2023
Create mutable copies of options passed to Processor.create...()
robertpanzer added a commit that referenced this issue Jun 22, 2023
Fixes #1221. 'UnsupportedOperationException' when passing immutable Map as options to 'createPhraseNode'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants