apache / apache/grails-core

CascadingSave & CascadingValidate unexpected behaviour

Open
#12,167 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

Hi,
We created a small application with two domains & respective RestfulControllers:

```
class Workshop{
String name
WorkshopCategory workshopCategory
}

class WorkshopCategory{
String name
}
```

and we are surprised to find, that the default deep-save behaviour cannot seem to be disabled.
So if we send the following JSON to the REST-endpoints, the category will first be created with the name "First category" and then, due to cascading save, be updated from the workshop endpoint to "First category RENAMED!":

// Create new Category

`POST /workshopCategory`
```
{
"name": "First category"
}

```

// Create new Workshop with nested workshopCategory
`POST /workshop`

```
{
"name": "First workshop",
"workshopCategory": {
"id": 1,
"name": "First category RENAMED!"
}
}
```

So we tried to disable this bevaviour in two ways:

`application.groovy`
```
grails.gorm.default.mapping = {
'*'(cascadeValidate: 'none')
}
```

and since that didnt work, we tried to add it to the Workshop Domain:

`Workshop.groovy`
class Workshop{
String name
WorkshopCategory workshopCategory

static mapping = {
workshopCategory(cascadeValidate: "none")
}
}

But neither prevents the cascading saving.
What are we missing here? We would like to prevent this from happening, i.e. if we explicitly call workshop.save() (as it happens in the RestfulController) then only that entity should save. We understand the other scenario might be very useful sometimes, but for our applications this could lead to unintended data overrides.

Any feedback here is greatly appreciated :)

### Environment Information

- **Operating System**: MacOS 10.15.7
- **Grails Version:** 5.0.0
- **JDK Version:** 11

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the two POST requests described in the report, then inspect the cascade settings in application.groovy and Workshop.groovy and the RestfulController save path. Done means saving a Workshop does not overwrite the existing WorkshopCategory, while the requested validation and save behavior is clearly covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.