You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Puneet Behl (Jira)" <ji...@apache.org> on 2020/12/18 13:46:00 UTC

[jira] [Commented] (GROOVY-9662) Groovy 3.0.5: Closure delegate is not working properly

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

Puneet Behl commented on GROOVY-9662:
-------------------------------------

This is also happening with Grails Framework after updating to Groovy 2.5.14, the following code does not behave the same with `CompileStatic`:
{code}

import groovy.json.JsonOutput

import groovy.json.StreamingJsonBuilder

import groovy.transform.CompileStatic

import org.springframework.http.HttpMethod

import org.springframework.validation.ObjectError

 

@CompileStatic

class VndErrorJsonRenderer { 

    final String LOGREF_ATTRIBUTE = 'logref'

    final String MESSAGE_ATTRIBUTE = "message"

    final String PATH_ATTRIBUTE = "path"

    final String RESOURCE_ATTRIBUTE = "resource"

    final String HREF_ATTRIBUTE = "href"

    final String LINKS_ATTRIBUTE = "_links"

    Writer targetWriter = new StringWriter()

 

    void render() { 

        StreamingJsonBuilder writer = new StreamingJsonBuilder(targetWriter)

        writer.call *{* ObjectError oe*->*

                **                writer

                call(LOGREF_ATTRIBUTE, "logref")

                call(MESSAGE_ATTRIBUTE, "msg")

                call(PATH_ATTRIBUTE, "http://localhost:8080")

                call(LINKS_ATTRIBUTE) *{*

                    **                    call(RESOURCE_ATTRIBUTE) *{*

                        **                        call(HREF_ATTRIBUTE, "path")

                    *}*

                *}*

        *}*

        **        targetWriter.flush()

    }

}

 

def vndErrorJsonRenderer = new VndErrorJsonRenderer()

vndErrorJsonRenderer.render()

 

JsonOutput._prettyPrint_(vndErrorJsonRenderer.targetWriter.toString())

 


{code}

> Groovy 3.0.5: Closure delegate is not working properly
> ------------------------------------------------------
>
>                 Key: GROOVY-9662
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9662
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 3.0.2, 3.0.3, 3.0.4, 3.0.5, 2.5.14
>            Reporter: Puneet Behl
>            Assignee: Eric Milles
>            Priority: Major
>         Attachments: JsonViewWritableScript.groovy, StreamingJsonBuilder.java
>
>
> In Grails Views, we are using closures to generate JSON using `StreamingJsonBuilder`. The HAL implementation for an entity now generates an incorrect JSON after updating to Groovy 3.
>  
> Here is the problem code snipped from \{{grails.plugin.json.view.api.internal.DefaultHalViewHelper#links}}: 
> {code:java}
> jsonDelegate.call(LINKS_ATTRIBUTE) {
>     call(SELF_ATTRIBUTE) {
>         call HREF_ATTRIBUTE, viewHelper.link(resource: object, method: HttpMethod.GET, absolute: true)
>         call HREFLANG_ATTRIBUTE, locale.toString()
>         call TYPE_ATTRIBUTE, contentType
>     }
>     Set<Link> links = getLinks(object)
>     for (link in links) {
>         call(link.rel) {
>             call HREF_ATTRIBUTE, link.href
>             call HREFLANG_ATTRIBUTE, link.hreflang?.toString() ?: locale.toString()
>             def linkType = link.contentType
>             if (linkType) {
>                 call TYPE_ATTRIBUTE, linkType
>             }
>         }
>     }
> }
> {code}
>  
> Although the `resolveStatergy` for the closures is `DELEGATE_FIRST` but I believe that is not working correctly because it works if I change the above to:
> {code:java}
> delegate.call (...
> {code}
>  
> To reproduce the issue please checkout [https://github.com/grails/grails-views/commits/bugs/groovy-9962]
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)