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

Grails views documentation is misleading and confusing #204

Open
3 tasks
woodmawa opened this issue Jan 22, 2019 · 2 comments
Open
3 tasks

Grails views documentation is misleading and confusing #204

woodmawa opened this issue Jan 22, 2019 · 2 comments

Comments

@woodmawa
Copy link

woodmawa commented Jan 22, 2019

Had a long play with Grails views for generating json. The documentation is misleading and probably erroneous in what its suggesting

based on explorations using grails 3.3.9/JsonView 1.2.10.

Task List

  • setup a normal grails project and a domain class
  • setup a controller and extend fro RestfulController
  • initially dont implement any gson views

Steps to Reproduce

  1. run the app - and point browser at localhost:8080/api/ (based on your url mappings)

this renders a result even though no views have been created (using some default thats not really explained )

  1. stop the app.

  2. create a gson view template as suggested on page 4. Assuming domain object is person the template shows is like this

//_person.gson

model {
   Person person
}
json {
    name person.name
    age person.age
}

also create an index.gson (for default browser get query). the documentation on bottom of page 5 sasy you can also pass an Iterable like this

//index.gson

model {
   List<Person> people = []
}
json tmpl.person(people)  //(person:people)
  1. restart the app - this clears the cache

now when you point your browser at localhost:8080/api/person (based on your url mappings)

you get an errors. First is that the 'people' variable in the index.gson cant me mapped to person in _person.gson template (model name mismatch. if you try and fix that using the mapped form it still errors as the PageResultList (List people) cant be passed to tmpl if the model variable is single Person.

  1. despite the RestfulController default index passing in both list of and model:[Count: list.size() to respond - if you add an Integer Count into the model it does not get data bound with the size

  2. middle of page 6. i t says by passing a collection, the plugin will iterate over each on the colelction and render the tmplate as json arrary. This just not work as documented.

Actual Behaviour

  1. I believe the documentation should clearly state the default behaviour is (if no gson views are defined)

  2. the example of _person.gson and show.gson needs to be rewritten more clearly - so its clear the show types have to be the same.

also in case of say index.gson (using List) that you have to interate and expand as required to exactly match the model name and type in any _templ.gson you write

In addition it should be made very clear that the view template needs to iterate through the list and call the _person template repeatedly to ensure the model variables are populated correctly

something like
//index.gson

model {
  List<Person> people = []
}
people.each {
     json tmpl.person (person:it)
}

Its taken forever to try and figure out how to get the right interplay between the controller action, the corresponding view and the tmpl and manage the respective types appropriately, and the confusion on what gets rendered if no gson views exist all

Environment Information

  • Operating System: windows 10
  • Grails Version: 3.3.9
  • JsonViews: 1.2.10
  • JDK Version: v1.181

Example Application

https://github.com/woodmawa/LCM-network-app

@jeffscottbrown
Copy link
Member

Where are the docs that indicate that the following should work?

model {
    List people = []
}

Thanks for your feedback.

@jeffscottbrown
Copy link
Member

I think that model variable should be List<Person> personList (or something more general like Iterable<Person> personList.

Thanks for the feedback. We will look at updating the docs.

@jeffscottbrown jeffscottbrown transferred this issue from grails/grails-core Jan 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants