Since DATAREST-1383, JSON fields mapped via @JsonAnySetter can no longer be updated via an http PATCH request.
I model i18n texts as simple JSON objects where the field name is the language tag and the field value the translation for that language, e.g.
{
"de": "Hallo",
"en": "Hello",
"fr": "Bonjour"
}
In Java, I model this as a class I18nText which holds a Map of translations, which is mapped to JSON via @JsonAnyGetter / @JsonAnySetter annotations:
class I18nText {
// JPA annotations, id field etc. ommitted for clarity
private Map<String, String> translations = new HashMap<String, String>();
@JsonAnySetter
private void set(String languageTag, String translation) {
// ...
}
@JsonAnyGetter
private Map<String, String> getTranslations() {
// ...
}
}
I18nText objects are then used in other entities, for example in a product:
class Product {
@OneToOne(...)
private I18nText name = new I18nText();
// ...
}
Updating such an object with http PATCH no longer works. Sind commit 28787116 DomainObjectReader#doMerge removes fields from the input JSON for which there are no persistent properties on the target object. In the above case, there is seemingly no persistent property for field "de" etc., and all translations (de, en, fr) get removed. As a consequence, the update gets lost. Furthermore, a custom JsonDeserializer doesn't work either, as the relevant fields have already been removed when it gets called
I have also hit this issue, while trying to mask a nested mongodb object from the clients via a JSONAnySetter, which works flawlessly, except for the update not working at all due to this issue.
As a (quite ugly) workaround I have set the Map<String,Object> field to public and set a @JsonFilter with SimpleBeanPropertyFilter.serializeAllExcept, it does the trick until there will be a proper fix for this
sth77 opened DATAREST-1440 and commented
Since DATAREST-1383, JSON fields mapped via
@JsonAnySetter
can no longer be updated via an http PATCH request.I model i18n texts as simple JSON objects where the field name is the language tag and the field value the translation for that language, e.g.
In Java, I model this as a class I18nText which holds a Map of translations, which is mapped to JSON via
@JsonAnyGetter
/@JsonAnySetter
annotations:I18nText objects are then used in other entities, for example in a product:
Updating such an object with http PATCH no longer works. Sind commit 28787116 DomainObjectReader#doMerge removes fields from the input JSON for which there are no persistent properties on the target object. In the above case, there is seemingly no persistent property for field "de" etc., and all translations (de, en, fr) get removed. As a consequence, the update gets lost. Furthermore, a custom JsonDeserializer doesn't work either, as the relevant fields have already been removed when it gets called
Affects: 3.2 GA (Moore)
Backported to: 3.2.1 (Moore SR1), 3.1.12 (Lovelace SR12)
The text was updated successfully, but these errors were encountered: