You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Andres Almiray (JIRA)" <ji...@apache.org> on 2017/02/07 13:11:42 UTC

[jira] [Commented] (GROOVY-7405) Templating with bound variables that match class names lead to unexpected results

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

Andres Almiray commented on GROOVY-7405:
----------------------------------------

It appears that the problem is related to the default imported brought by the Groovy compiler during the RESOLVE phase. The following script fails because `String` is resolved as `java.lang.String` but the `MarkupBuilder` expression is correctly resolve as "xml".


{code}
import groovy.text.*
import groovy.xml.MarkupBuilder
TemplateEngine engine = new GStringTemplateEngine()
Template template = engine.createTemplate('This gets incorrect bindings when a binding variable name matches a classname such as ${MarkupBuilder} or ${String}')
Map binding = ['Category': 'Test category', 'String': 'some string','MarkupBuilder': 'xml']
String evaluated = template.make(binding)
assert evaluated == 'This gets incorrect bindings when a binding variable name matches a classname such as xml category or some string'
{code}

output from groovyconsole

{code}
Assertion failed: 

assert evaluated == 'This gets incorrect bindings when a binding variable name matches a classname such as xml category or some string'
       |         |
       |         false
       This gets incorrect bindings when a binding variable name matches a classname such as xml or class java.lang.String

	at ConsoleScript12.run(ConsoleScript12:7)
{code}

> Templating with bound variables that match class names lead to unexpected results
> ---------------------------------------------------------------------------------
>
>                 Key: GROOVY-7405
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7405
>             Project: Groovy
>          Issue Type: Bug
>          Components: Templating
>    Affects Versions: 2.3.10
>            Reporter: Steven Walters
>            Priority: Minor
>
> I would expect that during binding, the binding occurs on supplied variables first. then after it does class lookups, but that is not what I'm finding:
> {code:title=Test.groovy|borderStyle=solid}
> //TemplateEngine engine = new SimpleTemplateEngine()
> TemplateEngine engine = new GStringTemplateEngine()
> Template template = engine.createTemplate('This gets incorrect bindings when a binding variable name matches a classname such as ${Category} or ${String}')
> Map binding = ['Category': 'Test category', 'String': 'some string']
> String evaluated = template.make(binding)
> assert evaluated == 'This gets incorrect bindings when a binding variable name matches a classname such as Test category or some string'
> {code}
> fails because {{Category}} gets mapped to {{interface groovy.lang.Category}} and {{String}} gets mapped to {{class java.lang.String}}
> This occurs for both {{GStringTemplateEngine}} and {{SimpleTemplateTemplate}}
> This may simply be an improper expectation on my part, but the behavior in this scenario is completely lacking from the documentation which lead to my possibly wrong expectation.
> I encountered this scenario when working on exposing the templating engine out to end-users to utilize a template to generate content for data being generated by a different team that has fairly arbitrary names and associated values.
> Honestly a bit skeptic as to this getting fixed to match the behavioral expectation I had, so instead expecting some documentation to provide clarity on the behavior going forward.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)