grails-core icon indicating copy to clipboard operation
grails-core copied to clipboard

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

Open johan313 opened this issue 1 year ago • 13 comments

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

johan313 avatar Jul 14 '22 14:07 johan313

Seems like I'm supposed to use Micronaut HTTP client https://docs.grails.org/latest/guide/REST.html#callingRestServices nowdays. However, the documentation seem to be out-of-date = non compiling and the Micronaut Client returns the Publisher interface which is not needed for m2m calls. This could be updated.

johan313 avatar Jul 16 '22 11:07 johan313

Does the code at https://github.com/jeffbrown/johan313issue12623/blob/d1797f4cb81184c1a703ce699c52644228de03ae/grails-app/controllers/johan313issue12623/DemoController.groovy#L6 throw a NoSuchMethodError in your environment?

osscontributor avatar Jul 16 '22 12:07 osscontributor

I see the NoSuchMethodError when responding with a domain instance like you show:

respond MyEntity.get(id), formats: ['json']

But I don't understand why you are seeing it with this:

respond "value", formats: ['json']

I would expect a different problem with that.

Thank you for your feedback.

osscontributor avatar Jul 16 '22 12:07 osscontributor

Seems like I'm supposed to use Micronaut HTTP client

I don't think an http client is involved in the problem described in this question. I think the issue is incompatible libraries in the dependency graph of the app.

osscontributor avatar Jul 16 '22 12:07 osscontributor

Yes, the problem is incompatible libraries, but I assumed the cause was that I was not supposed to use grails-datastore-rest-client anymore.

johan313 avatar Jul 16 '22 12:07 johan313

Does the code at https://github.com/jeffbrown/johan313issue12623/blob/d1797f4cb81184c1a703ce699c52644228de03ae/grails-app/controllers/johan313issue12623/DemoController.groovy#L6 throw a NoSuchMethodError in your environment?

You still need me to check this? I can do it on Monday if so.

johan313 avatar Jul 16 '22 12:07 johan313

I would expect a different problem with that.

Yes, I get a different exception with that, I get

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

johan313 avatar Jul 16 '22 12:07 johan313

@johan313 Did you find a solution or work around for this issue?

donalmcnamee avatar Jan 05 '23 10:01 donalmcnamee

@donalmcnamee

Didn't want to waste time on an outdated plugin so I removed grails-datastore-rest-client. Switched to Spring REST client for complex cases and the standard java.net.http package for simple, chose those because they are actively maintained in the foreseeable future.

johan313 avatar Jan 05 '23 12:01 johan313

Service: TimeSheetWeekService Method : prepareSubmissions

I have concluded that the problem is due to Multimap<String, Object> result = ArrayListMultimap.create() I'll try to fix it tomorrow.

naeemghumman avatar Jan 10 '23 19:01 naeemghumman

@naeemghumman , please let me know any update on this issue. we are using Grails 6.1.0 and getting error rendering Multimap<String, List<? extends Object>> as JSON, it has dependency on org.grails:grails-datastore-rest-client:6.1.9.RELEASE.

kmpjsharma avatar Dec 27 '23 15:12 kmpjsharma

@kmpjsharma, I am no longer working on the project where I was getting this issue.

naeemghumman avatar Dec 27 '23 18:12 naeemghumman

I used GSON to convert Multimap<String, List<? extends Object>> as JSON Gson gson = new Gson(); String jsonStr = gson.toJson(eventsData,LinkedHashMap.class); render jsonStr

kmpjsharma avatar Jan 02 '24 21:01 kmpjsharma