0

When I use RestfulController in Grails to save data for an object, how can I prevent the client from applying changes to a related child object?

Given the following domain classes:

class Language {
    String name
}

class TranslationText {
    Language language
    String text
}

And the following POST data for a TranslationText:

{
    "language": { "id": 1, "name": "InvalidName" },
    "text": "Some Text"
}

Here, I want to reference an existing Language resource (with ID=1), but I don't want the name to be altered by the client.

How can I save this resource with the text and language (based on ID), but discard the invalid language name property?

I want to modify RestfulController in the most minimal way possible, preserving default behavior as much as I can.

RMorrisey
  • 7,637
  • 9
  • 53
  • 71
  • You can use `bindable` constraint if you don't want the `name` property to be assigned. http://docs.grails.org/latest/ref/Constraints/bindable.html – Hardik Modha Sep 29 '17 at 16:35
  • If I map this on the language class, won't this prevent me from binding it when I post to the LanguageController? – RMorrisey Sep 29 '17 at 17:05

1 Answers1

0

I think you need to configure the 'cascade' mapping property. This will tell GORM to evict the linked instance, so it won't be in the Hibernate session, changed to a new name and flushed to the DB :

class TranslationText {
    Language language
    String text

    static mapping = {
        language cascade: 'evict'
    }
}

ref : http://docs.grails.org/3.1.x/ref/Database%20Mapping/cascade.html

bassmartin
  • 525
  • 2
  • 9