You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Cyril de Catheu (Jira)" <ji...@apache.org> on 2023/01/14 20:53:00 UTC

[jira] [Commented] (TEXT-223) StringSubstitutor checks for cycles even if recursive substitution is disabled

    [ https://issues.apache.org/jira/browse/TEXT-223?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17676945#comment-17676945 ] 

Cyril de Catheu commented on TEXT-223:
--------------------------------------

I misunderstood the usage of `{{{}[setEnableSubstitutionInVariables|https://commons.apache.org/proper/commons-text/apidocs/org/apache/commons/text/StringSubstitutor.html#setEnableSubstitutionInVariables-boolean-]{}}}` and `{{{}[setDisableSubstitutionInValues|https://commons.apache.org/proper/commons-text/apidocs/org/apache/commons/text/StringSubstitutor.html#setDisableSubstitutionInValues-boolean-]{}}}`. 
In my case, it's substitution in values that should be disabled.


import java.util.Map;import org.apache.commons.text.StringSubstitutor;public class TestCycles {
  public static void main(String[] args) {
    Map<String, Object> cycleMap = Map.of("cycle", "${cycle}");
    StringSubstitutor sub = new StringSubstitutor(cycleMap).setDisableSubstitutionInValues(true);
    String res = sub.replace("my template ${cycle}");
  }
}

> StringSubstitutor checks for cycles even if recursive substitution is disabled
> ------------------------------------------------------------------------------
>
>                 Key: TEXT-223
>                 URL: https://issues.apache.org/jira/browse/TEXT-223
>             Project: Commons Text
>          Issue Type: Bug
>    Affects Versions: 1.10.0
>            Reporter: Cyril de Catheu
>            Priority: Minor
>
> *Issue* 
> StringSubstitutor can perform [recursive variable replacements|https://commons.apache.org/proper/commons-text/apidocs/org/apache/commons/text/StringSubstitutor.html#:~:text=recursive]. 
> If there is a cycle, an IllegalStateException is thrown.
> When using StringSubstitutor with recursive substitution disabled, an exception is still thrown in case of cycles.
> *Expected behavior*
> No exception should be thrown.  There is no cycle because the replacement is not recursive.
> *How to reproduce*
> {code:java}
> import java.util.Map;
> import org.apache.commons.text.StringSubstitutor;
> public class TestCycles {
>   public static void main(String[] args) {
>     Map<String, Object> cycleMap = Map.of("cycle", "${cycle}");
>     StringSubstitutor sub = new StringSubstitutor(cycleMap).setEnableSubstitutionInVariables(false);
>     String res = sub.replace("my template ${cycle}");
>   }
> }
> {code}
> Will throw
> {code:java}
> java.lang.IllegalStateException: Infinite loop in property interpolation of my template ${cycle}: cycle.{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)