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

Empty "includes" Array Causes Leading comma in JSON payload within g.render(...) #451

Open
econnatz opened this issue Jun 29, 2023 · 0 comments
Assignees

Comments

@econnatz
Copy link

econnatz commented Jun 29, 2023

Plugin version: 2.3.2
Versions of Grails tested: 5.2.5, 5.3.2

The example below produces invalid JSON (due to the leading comma):

Sample Test Gson File

import com.test.TestGsonController

model {
    List<TestGsonController.TestUser> content
}

json {
    content g.render(content, [includes:[]]) { TestGsonController.TestUser testUser ->
        firstName testUser.firstName
        lastName testUser.lastName
    }
}

Sample Controller File

package com.test

class TestGsonController {
    def index() {
        List<TestUser> contentList = [
                new TestUser(['firstName':'John','lastName':'Doe','ssn':119]),
                new TestUser(['firstName':'Jane','lastName':'Doe','ssn': 120])
        ]

        render view: '/test-gson/index', model: [content: contentList]
    }

    static class TestUser {
        String firstName
        String lastName
        Integer ssn
    }
}

JSON OUTPUT

{"content":[{,"firstName":"John","lastName":"Doe"},{,"firstName":"Jane","lastName":"Doe"}]}

BEHAVIOR ANALYSIS

If we have gson file that calls "render" with an empty includes array, it renders incorrect JSON like what is shown above as long as what's being rendered is an object that's not a map AND has a render body closure. The render logic seems to assume that there will be at least one include field, in which there would be a need for the leading comma since the include field(s) are rendered before the ones manually defined in the render closure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants