Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSON problem: custom adapters not used #36

Open
sergiomichels opened this issue Jun 5, 2013 · 12 comments
Open

GSON problem: custom adapters not used #36

sergiomichels opened this issue Jun 5, 2013 · 12 comments

Comments

@sergiomichels
Copy link

For some reason GSON is failing in get the GsonBuilder from the applicationContext, this causes the class to return a new GsonBuilder() without my custom Adapters. For example:

@Lazy
private GsonBuilder gsonBuilder = {
  GsonBuilder gsonBuilder = applicationContext?.getBean('gsonBuilder', GsonBuilder)
  if(gsonBuilder) {
    println "**** Using applicationContext GsonBuilder"
  } else {
    println "**** Not found bean gsonBuilder"
    gsonBuilder = new GsonBuilder()
  }
  return gsonBuilder
}()

In my case it prints "Not found bean gsonBuilder" and by consequence using render model as GSON fails to use my adapters.

The problem goes away if I explicity declare gsonBuilder in my controller and use render contentType: 'application/json', text: gson.toJson(model)

Maybe it's a problem with ApplicationContextAware? I can also see that applicationContext is null in GSON.

@uris77
Copy link

uris77 commented Jun 6, 2013

I'm experiencing the same problems.

@sergiomichels
Copy link
Author

What's the diff between ApplicationContextAware and setting applicationContext in the bean definition? Maybe the issue is with ApplicationContextAware?

@quidryan
Copy link

I believe when going through the GroovyDefaultMethods asType calls, it just constructs the GSON object. Since it never passes through Spring and applicationContext is never set. Without access to the applicationContext, there's no hope that the gsonBuilder can get set.

I think the orignal idea was that since the ArtefactEnhancer is adding the render method and since it has a proper gson object, this could work. But the variable is defined both at the class level and as the argument the closure. Since the argument to the closure is bullocks, we should be using it, but it's scoped as the variable to use. I think the fix is remove the arg from the closure in enhanceControllers(). I'll try locally and see what happens.

@quidryan
Copy link

ConvertersDomainTransformer seems responsible for properly creating the GSON converter, but only for Domain objects, not on Controllers.

@robfletcher
Copy link
Owner

Is this a problem with accessing the spring context from a static context when using as GSON? I think the reason it's going down that path is that applicationContext is null – note the ?. operator.

@sergiomichels
Copy link
Author

@robfletcher It's just a controller action, not a static method.

@robfletcher
Copy link
Owner

@sergiomichels yeah, the static-ness is in the gson plugin

@sergiomichels
Copy link
Author

@robfletcher I think that using Holders.grailsApplication.mainContext will solve the issue. I will do some tests.

@sergiomichels
Copy link
Author

hmmm I'm getting a hard time trying to reproduce this again.

@angelo-v
Copy link

This is still an issue, I have the same problem: applicationContext is null in GSON when using

render ([date: new Date()] as GSON

My custom DateAdapter is not used in this case.

@sergiomichels
Copy link
Author

I made a suggestion in #35.

@robfletcher
Copy link
Owner

This is potentially solve alb using grails.util.Holders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants