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-Views can't handle immutability? #424

Open
davidkron opened this issue Nov 23, 2022 · 0 comments
Open

GSON-Views can't handle immutability? #424

davidkron opened this issue Nov 23, 2022 · 0 comments
Assignees

Comments

@davidkron
Copy link
Contributor

davidkron commented Nov 23, 2022

We integrated grails-views into an older existing application to provide a good JSON developer experience for newly created and modern single-page applications. We already had some JSON-Endpoints that render a simply model using the Grails JSON marshaller like this:

respond(result, formats: ['json'])

A lot of those broke in our application and the reason seems to be that grails-views can't handle immutable models, which seems like a major flaw in this library. The code where this is explicitly implemented is right here:
https://github.com/grails/grails-views/blob/2.3.x/json/src/main/groovy/grails/plugin/json/view/api/internal/DefaultGrailsJsonViewHelper.groovy#L387

For grails-views we also implemented the default template /grails-app/views/object/_object.gson exactly as described in the documentation, as we do want to fallback to a default behavior, when no explicit gson-view is available. When this view is not found it seems to work, as the fallback is to use the Grails JSON marshaller (like when not using grails-views). But this is a workaround not a real solution.

This following code doesn't produce the expected JSON result:

class TestController {
    def index() {
        def result = new MyResult("Hello World!")
        respond(result, formats: ['json'])
    }
}

@TupleConstructor
class MyResult {
    final String text
}

Response:
{}

If I remove the final from the model it works:

@TupleConstructor
class MyResult {
    String text
}

Response:

{
    "text": "Hello World!"
}
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

2 participants