grails / grails-views

Additional View Technologies for Grails
Apache License 2.0
56 stars 41 forks source link

Customization of self link inside hal.embedded doesn't override default, creating two repeated _links elements with their own self's #124

Open reinaldoluckman opened 7 years ago

reinaldoluckman commented 7 years ago

I have these three resources:

abstract class HalResource {
    String selfLink
    HalResource(String _selfLink) {
        this.selfLink = _selfLink
    }
}
class TrainingElementResource extends HalResource {

    String image
    String name
    String description

    TrainingElementResource(String _image, String _name, String _description, String _selfLink) {

        super(_selfLink)

        this.image = _image
        this.name = _name
        this.description = _description
    }
}
class TrainingListResource extends HalResource {

    Collection<TrainingElementResource> trainings

    TrainingListResource(String _selfLink) {
        super(_selfLink)
        this.trainings = []
    }
}

When I try to customize self links from those resources inside in a gson file (see bellow), the self link for root resource (TrainingListResource) works without problem, but, that inside hal.embedded, doesn't overwrites the plugin default. Is this a expected behavior?

model {
    TrainingListResource trainingListResource
}

json {

    hal.links(self: trainingListResource.selfLink)
    hal.embedded {
        trainings (trainingListResource.trainings) { TrainingElementResource t ->
            hal.links(self: t.selfLink)
        }
    }
}

The output json:

{  
   "_links":{  
      "self":{  
          // ------> THIS IS OK IN ROOT RESOURCE
         "href":"http://localhost:8080/api/v1/training",
         "hreflang":"en_US",
         "type":"application/hal+json"
      }
   },
   "_embedded":{  
      "trainings":[  
         {  
            "_links":{  
               "self":{
                   // ------> THIS IS NOT  EXPECTED; DEFAULT SELF LINK
                  "href":"http://localhost:8080/trainingElementResource%40713cf590/index",
                  "hreflang":"en_US",
                  "type":"application/hal+json"
               }
            },
            "_links":{  
               "self":{
                  // ------> THIS IS WHAT I WANT  
                  "href":"http://localhost:8080/api/v1/training/meta-learning",
                  "hreflang":"en_US",
                  "type":"application/hal+json"
               }
            }
         },
         {  
            "_links":{  
               "self":{  
                   // ------> THIS IS NOT  EXPECTED; DEFAULT SELF LINK
                  "href":"http://localhost:8080/trainingElementResource%40340493a7/index",
                  "hreflang":"en_US",
                  "type":"application/hal+json"
               }
            },
            "_links":{  
               "self":{  
                  // ------> THIS IS WHAT I WANT  
                  "href":"http://localhost:8080/api/v1/training/iq",
                  "hreflang":"en_US",
                  "type":"application/hal+json"
               }
            }
         }
      ]
   }
}

If this is a expected behavior (which is odd to me), there is someway to achieve self customization inside hal.embedded?

Thanks in advance.

reinaldoluckman commented 7 years ago

I solved the problem creating _embedded by myself:

model {
    TrainingListResource trainingListResource
}

json {

    hal.links(self: trainingListResource.selfLink)

    _embedded {

        trainings (trainingListResource.trainings) { TrainingElementResource t ->
            hal.links(self: t.selfLink)
            image t.image
            name t.name
            description t.description
        }
    }
}

But I will let open to get some direction.

Thank you in advance.