jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Support @JsonAnySetter on method with single Map parameter

Open cowtowncoder opened this issue 11 months ago • 5 comments

Discussed in https://github.com/FasterXML/jackson-databind/discussions/4888

Originally posted by chrylis January 7, 2025 Currently, @JsonAnySetter can be used on a physical field of type Map, but unlike @JsonProperty, it will be silently dropped if used on a method of arity other than two (POJOPropertiesCollector#_addMethods). I would like to be able to use @JsonAnySetter on a method representing a virtual property, like this:

@Slf4j
class WebhookDto {
  @JsonAnySetter
  void setUnmappedProperties(Map<String, Object> unmapped) {
    log.warn("unmapped properties on webhook: {}", unmapped)
  }
}
```</div>

cowtowncoder avatar Jan 07 '25 18:01 cowtowncoder

Hi, is it still available ? I'd like to tackle it as part of a software engineering course, we'll be multiple people working on it but I'll serve as intermediary not to burden you with that.

alixpeigue avatar Feb 26 '25 14:02 alixpeigue

@JooHyukKim What do you think about this wrt feasibility/need? You have been working most on any-getter/any-setter support.

@alixpeigue Yes, should be free: if someone was working on it, they'd be expected to add a note here.

cowtowncoder avatar Mar 05 '25 05:03 cowtowncoder

@cowtowncoder Hmmm this usage seems more like workaround, honestly. Adding custom handling for un-expected property isn't what @JsonAnySetter is for I think?

@alixpeigue Speaking of "need" along with "priority", we have couple of other JsonAnySetter related issues, if you trying to work on somethiing.

JooHyukKim avatar Mar 09 '25 12:03 JooHyukKim

@JooHyukKim I think use of @JsonAnySetter for properties for which there is no Field or Setter (hence, "unmapped") is pretty much the main use case?

cowtowncoder avatar Mar 09 '25 23:03 cowtowncoder

@JooHyukKim I think use of @JsonAnySetter for properties for which there is no Field or Setter (hence, "unmapped") is pretty much the main use case?

Yeah, we can see it that way makes sense. My idea was we might be better off creating hook to trigger when facing unknown properties. That way we dont even need JsonAnySetter or limit our selves to a builder like

'''Java JsonMapper.builder().onUnknwonProperty(logger::error).build() '''

JooHyukKim avatar Mar 10 '25 06:03 JooHyukKim