apache / apache/grails-core

Responding with JSON not working after upgrade to version 5.X from 4.X

Open
#12,623 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Expected Behavior

A JSON response is returned to the caller.

### Actual Behaviour

Exception is thrown.

```
NoSuchMethodError occurred when processing request: [GET] /admin/test
'grails.core.GrailsDomainClassProperty grails.core.GrailsDomainClass.getIdentifier()'. Stacktrace follows:
...
...
Caused by: java.lang.NoSuchMethodError: 'grails.core.GrailsDomainClassProperty grails.core.GrailsDomainClass.getIdentifier()'
at org.grails.web.converters.marshaller.json.DomainClassMarshaller.marshalObject(DomainClassMarshaller.java:116)
at org.grails.web.converters.marshaller.json.DomainClassMarshaller.marshalObject(DomainClassMarshaller.java:52)
at grails.converters.JSON.value(JSON.java:184)
at grails.converters.JSON.convertAnother(JSON.java:144)
at org.grails.web.converters.marshaller.json.CollectionMarshaller.marshalObject(CollectionMarshaller.java:41)
at org.grails.web.converters.marshaller.json.CollectionMarshaller.marshalObject(CollectionMarshaller.java:30)
at grails.converters.JSON.value(JSON.java:184)
```

### Steps To Reproduce

Add the following dependency to a clean grails project:

implementation "org.grails:grails-datastore-rest-client:6.1.12.RELEASE"

The following code produces a no NoSuchMethodError when I call the endpoint:

def getSomething(final Long id) {
respond MyEntity.get(id), formats: ['json']
}

or even easier is:

def test() {
respond "value", formats: ['json']
}

Results in Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'value' with class 'java.lang.String' to class 'grails.converters.JSON'.

This is caused by the dependency "org.grails:grails-datastore-rest-client:6.1.12.RELEASE", without it everything works fine. Please advice on a workaround or release a fix for the dependency.

### Environment Information

- Windows
- JDK 15.0.2
- Grails 5.2.1

### Example Application

_No response_

### Version

5.2.1

Contributor guide

Open the contributing guide

Research direction

Start with the stack trace in org.grails.web.converters.marshaller.json.DomainClassMarshaller.java and the related grails.converters.JSON.java calls. Reproduce the failure in the clean Grails project using grails-datastore-rest-client:6.1.12.RELEASE and the getSomething or test endpoint. Done means JSON responses work without the NoSuchMethodError or GroovyCastException when that dependency is present.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.