apache / apache/grails-core

Regression in the Grails 4.0.6 calling response.json in Specification

Open
#14,275 5 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-views
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

When calling `response.json` in a Specification to test a RestfulController that `respond` with an empty list, an error occurs if you have created a default `/object/_object.gson` view such as:

```
model {
Object object
}
json g.render(object)
```

Condition failed with Exception:

```
response.json.size() == 0
| |
| org.grails.web.converters.exceptions.ConverterException: Error parsing JSON
| at grails.converters.JSON.parse(JSON.java:260)
| at org.grails.plugins.testing.AbstractGrailsMockHttpServletResponse.getJson(AbstractGrailsMockHttpServletResponse.groovy:93)
| at example.FooControllerSpec.test something(FooControllerSpec.groovy:20)
| Caused by: org.grails.web.json.JSONException: Missing value. at character 0 of
| at org.grails.web.json.JSONTokener.syntaxError(JSONTokener.java:481)
| at org.grails.web.json.JSONTokener.nextValue(JSONTokener.java:358)
| at grails.converters.JSON.parse(JSON.java:251)
| ... 2 more

```

Without the default GSON view, it works ok.

If you change the grails version to 4.0.5, the test will pass successfully, so this seems to be a regression in 4.0.6.

### Task List

- [x] Steps to reproduce provided
- [x] Stacktrace (if present) provided
- [x] Example that reproduces the problem uploaded to Github
- [x] Full description of the issue provided (see below)

### Steps to Reproduce
Download the sample application and run:

- git clone https://github.com/longwa/grails-gson-view-issue
- gradle test

The key is to call respond for a JSON format without an explicit GSON view to render.

### Expected Behaviour

`response.json` should succeed and essentially return empty (size == 0) as it does in 4.0.5.

### Actual Behaviour

An error occurs when `JSON.parse()` is called b/c the output stream has an empty string instead of empty array syntax: `""` vs `"[]"`. This seems to be a result of the `_object.gson` being used, which causes the renderer to follow a different path.

### Environment Information

- **Operating System**: macOS 10.15.7
- **Grails Version:** 4.0.6
- **JDK Version:** zulu-1.8_275

### Example Application

- https://github.com/longwa/grails-gson-view-issue

Contributor guide

Open the contributing guide

Research direction

Clone the linked grails-gson-view-issue example and run its gradle test to reproduce the failure. Start in grails.plugins.testing.AbstractGrailsMockHttpServletResponse.groovy at getJson and compare the rendering behavior between Grails 4.0.5 and 4.0.6. Done means response.json succeeds and reports size 0 when respond renders an empty list with the default _object.gson view.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.