apache / apache/grails-core

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

Open
#14,207 0 comments 0 reactions 1 assignee Claimed by @Richardson-e View on GitHub
relates-to: grails-views
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

**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 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 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.