Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields mapped with @JsonAnySetter are no longer updatable with http PATCH [DATAREST-1440] #1800

Closed
spring-projects-issues opened this issue Oct 18, 2019 · 2 comments
Assignees
Labels
in: repository type: regression

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Oct 18, 2019

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.

{
   "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


Affects: 3.2 GA (Moore)

Backported to: 3.2.1 (Moore SR1), 3.1.12 (Lovelace SR12)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 25, 2019

evfool commented

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.

 

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 25, 2019

evfool commented

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository type: regression
Projects
None yet
Development

No branches or pull requests

2 participants