Empty "includes" Array Causes Leading comma in JSON payload within g.render(...)
- 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
Assessment
This issue has not been assessed yet.