title: Grails 3 profiles,JSON rendering and One to Many with self
body: <p>I'm working with gson and web profile.</p>
  
  <p><strong>My domain is:</strong></p>
  
  <pre><code>package json
  import grails.rest.*
  
  @Resource(readOnly = false, formats = ['json', 'xml'])
  class Hero {
      String name
      String data
      String relation
      Book book
      static hasMany = [children: Hero]
  </code></pre>
  
  <p><strong>My controller is:</strong></p>
  
  <pre><code>package json
  
  import grails.rest.*
  import grails.converters.*
  
  class HeroController extends RestfulController {
      static responseFormats = ['json', 'xml']
      HeroController() {
          super(Hero)
      }
      def show(Hero hero){
          respond hero
      }
  }
  </code></pre>
  
  <p><strong>My gson:</strong></p>
  
  <p><strong>hero.gson</strong></p>
  
  <pre><code>import json.Hero
  
  model {
      Hero hero
  }
  
  json tmpl.hero(hero)
  </code></pre>
  
  <p><strong>_hero.gson</strong></p>
  
  <pre><code>import json.Hero
  
  model {
      Hero hero
  }
  
  json {
      //data hero.data
      id hero.id
      data(relation: hero.relation)
      name hero.name
      children g.render(hero.children)
  }
  </code></pre>
  
  <p>If I run it with restful profile then the all children nodes are rendered correctly.
  If I use web profile only two levels deep are rendered.</p>
  
  <p>My expected result is:</p>
  
  <pre><code>{
      "id": 4,
      "data": {
          "relation": "e"
      },
      "name": "e",
      "children": [{
              "id": 2,
              "data": {
                  "relation": "c"
              },
              "name": "c",
              "children": [{
                      "id": 1,
                      "data": {
                          "relation": "b"
                      },
                      "name": "b",
                      "children": []
                  }
              ]
          }
  </code></pre>
  
  <p>Is it possible to do one to many json rendering (as with the restful profile)?
  Is there a way to control depth of the rendering?</p>
  
  <p>P.S. I read the documentation and this part is not very clearfor me:</p>
  
  <blockquote>
    <p>If you wish for the author to be included as part of the rendering,
    there are two requirements, first you must make sure the association
    is initialized.</p>
    
    <p>If the render method encounters a proxy, it will not traverse into the
    relationship to avoid N+1 query performance problems.     The same
    applies to one-to-many collection associations. If the association has
    not been initialized the render method will not traverse through the
    collection!</p>
    
    <p>So you must make sure your query uses a join:</p>
    
    <p>Book.findByTitle("The Stand", [fetch:[author:"join"]])</p>
    
    <p>Secondly when calling the render method you should pass the deep
    argument:</p>
    
    <p>json g.render(book, [deep:true])</p>
  </blockquote>