Search code examples
grailsgrails-3.3.x

Include boolean params with g:include are passed as Strings to the included view


I don't know if the following change is an issue or it is intended.

<g:include view="line.gsp" params="['label':'test', 'progress':false]"/>

Then the expression in line.gsp aways evaluates to true, because the type of the 'progress' param is String, not Boolean.

  class is: ${params.progress.getClass()}
  <g:if test="${params.progress}">
   this should not be displayed
 </g:if>

Note that the same is applicable for other types, not just Boolean. I am using grails 3.3.8 This didn't happen in grails 2.5.2.

I didn't find anything about this online so that's why I am asking here. Thanks.

Edit:

As suggested by Daniel, I've tried with grails 3.3.2 also. I just created an app with grails create-app and modified the existing index.gsp to include line.gsp, as shown in the code above.

Here is a screenshot: enter image description here


Solution

  • I realize you have already found a workaround to this, but it was bothering me that your code was not working the same way mine was, so I decided to investigate a bit further in case anyone else runs into this issue.

    As other people here have mentioned, params.anything will typically return a String value. This is because params are typically encoded as URI parameters (like ?progress=false) and not autoboxed into any other type. (This is perfectly reasonable; there would be no good way for Grails to know what type they should be.) However, it is possible (and occasionally reasonable) to render your view or template from a controller like render view: whatever, model: [your: model, params: params] where you specifically include params in the model. (Perhaps you have a lot of parameters that you do not need to individually recreate in the model.) In this case, the params map will exist as both URI parameters (?progress=false) and page-scoped model entry (params: [progress: Boolean.FALSE]). Page scope takes priority over URI parameters, and so your types will be preserved.

    In my testing, I added your code to an existing page where I was already passing params into the model, and so type was preserved for a newly added parameter as well. (Note that once params are in page scope, they're there for included templates or views as well.) Consequently, I saw progress type of Boolean, while in a basic example it would be type String.

    TL/DR: either assume params are strings, or explicitly include params in your page-scoped model when rendering.