0

I have looked at this Grails get child domain objects but I am still lost.

I have added to conf/application.yml

converters:
    encoding: UTF-8
    grails.converters.json.default.deep: true

But when I do a get on a domain, I still get

[~]$ curl http://localhost:8080/request/2 {"id":2,"stream":{"id":2},"release_label":"ABC_4.3","date_created":"2017-08-21T13:06:27Z","envs":[{"id":1}],"status":"init"}

I want stream and envs to be expanded to give all of the records, not just the id.

So Request,groovy is

And Stream.groovy is

package test
import grails.rest.*
class Stream {
    String name
    String feedgroup
    String description
    UnixGroup unixgroup
    String swid
    boolean powercentre = false
    String latest_release
    static hasMany = [envs: Env]

    static constraints = {
          name blank:false, unique: true
    }
}

package test
import grails.rest.*

class Request {
    Date date_created = new Date()
    Date date_completed
    String status = "init"
    String release_label
    Stream stream
    static hasMany = [envs: Env]

    static constraints = {
           date_completed nullable: true
    }
        static searchable = {
            only = [ 'stream', 'status' ]
        }
}

I am using Grails 3.30.

Is grails.converters.json.default.deep still valid for Grails 3? And how do I use it.

John
  • 1,593
  • 3
  • 17
  • 28

2 Answers2

1

I have been told on the grails slack page that converters are not used when the application profile is rest-api.

Instead JSON views should be used. In the render, add an option deep: true

eg

json g.render(book, [deep:true])

where book is the domain

For further info, see http://views.grails.org/latest/#_rendering_domain_classes

John
  • 1,593
  • 3
  • 17
  • 28
0
grails.converters.json.default.deep

Should be changed to just:

json.default.deep

as you are already in the grails.converters block.

So it should look like:

converters:
    encoding: UTF-8
    json.default.deep: true
LeslieV
  • 745
  • 2
  • 13
  • 20
  • Tried the above and still no full child information in output. Also tried it as json: default: deep: true – John Aug 22 '17 at 07:35
  • I setup a couple of sample domains similar to what you provided and tested this. I see the child info in the output: `curl http://localhost:8080/sample` `{"request":{"id":1,"stream":{"id":1,"feedgroup":"s2","name":"s1","envs":[{"id":1,"title":"title1","name":"env1"}]},"release_label":"label1","date_created":"2017-08-23T01:39:36Z","envs":[{"id":2,"title":"title2","name":"env2"},{"id":1,"title":"title1","name":"env1"}],"date_completed":null,"status":"init"}}` Maybe try a gradle:clean first, else maybe post more of your .yml/code. – LeslieV Aug 23 '17 at 01:52
  • Hi LeslieV - thank you for the work you are putting into this. I have cleaned and run-app again and still same result. Would any other section of application.yml have an affect on this? I can't think what I am doing different to yourself. I am not using @Resource in the domain but have generated controllers. So I notice that your curl uses the application name whereas I use the record type name. – John Aug 23 '17 at 09:05
  • Hi @john, I guess it is possible that something else in your application.yml could be affecting this. If you'd like to post it in its entirety, I can take a look. I am using the `sample` in my cURL command is hitting the SampleController I setup. – LeslieV Aug 23 '17 at 16:03
  • Would there be something in your controller that is helping to give the right result? My application.yml is the default created during create-app except for json.default.deep – John Aug 23 '17 at 16:33
  • Actually could you post your UrlMapping.groovy file – John Aug 24 '17 at 05:46
  • Keep in mind that this was a very quick sample app... but I just opened up my GitHub repo that has this code, so you can see it: https://github.com/lviviani/child-domain-json – LeslieV Aug 25 '17 at 01:47
  • Leslie, did you create your app with a profile of rest-qpi or as a default web-app? – John Aug 29 '17 at 15:37
  • Just the default ... `grails create-app` – LeslieV Aug 29 '17 at 17:31