apache / apache/grails-core

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

Open
#14,303 1 comment 0 reactions 0 assignees View on GitHub
relates-to: grails-views
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

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 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.

Contributor guide

Open the contributing guide

Research direction

Start with the JSON configuration, especially the hal.embedded block and its nested hal.links(self: t.selfLink) entry, then inspect how embedded resources are serialized. Done means each embedded training has one _links object whose self href uses the customized resource link rather than the generated default.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.