apache / apache/grails-core

Auto Mapping of Dynamic fields in RestfulController POST (save) method

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

Description

Hi Grails Team,

How do I enable RestfulController to auto-map the dynamic fields to domain classes implementing MongoEntity? I have a domain class as below:

```
class Company implements MongoEntity {
String id = UUID.randomUUID().toString()
String name
String email
String phone
}
```

And I have a RestfulController setup for CRUD operations as below

```
class CompanyController extends RestfulController {

@Transactional
def save(Company company) {
if(company.hasErrors()) {
respond company.errors
}
else {
company.insert(flush:true)
respond company, status: CREATED
}
}
}
```
When I POST a request with some additional JSON fields, how do I get them auto-mapped to gorm_dynamic_attributes ? Currently the company object does not return any information on the dynamic attributes. Another problem I am facing is that `request.JSON` is also null so I cannot manually map either.

Please suggest.

Thanks.

Contributor guide

Open the contributing guide

Research direction

Start with the CompanyController save method and the RestfulController POST binding path, then inspect how MongoEntity handles gorm_dynamic_attributes and why request.JSON is null. Done means additional JSON fields are mapped to the domain object and are present in the response, with the behavior covered by an appropriate test.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.