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

Add `ElementType.PARAMETER` to `@JsonIgnore` to allow use for Constructor parameters

Open cowtowncoder opened this issue 1 year ago • 7 comments

(note: off-shoot of https://github.com/FasterXML/jackson-databind/issues/4626)

Looks like @JsonIgnore cannot be used on method/constructor parameters: latter can be problematic with Record types in particular (but also regulard POJOs' Constructors or Factory methods). So let's add that in 2.18.

cowtowncoder avatar Jul 21 '24 21:07 cowtowncoder

One would expect Kotlin Data Class to handle annotations the same as Java Records, i.e. propagate annotation to fields and/or getters and/or constructor parameters depending on @Target, but apparently not.

Looking at the decompiled Kotlin Data Class, something like this:

data class BooleanPropertyInBody(@JsonIgnore val placeholder: String = "placeholder") { ... }
  • Before this change: @JsonIgnore added on the generated field.
  • After this change: @JsonIgnore added only on the constructor parameter. 🤨

UPDATE

Annoying - from https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets:

If you don't specify a use-site target, the target is chosen according to the @Target annotation of the annotation being used. If there are multiple applicable targets, the first applicable target from the following list is used:

  • param
  • property
  • field

yihtserns avatar Jul 22 '24 01:07 yihtserns

Ugh, I missed out an important fact in https://github.com/FasterXML/jackson-databind/issues/4626:

  1. @JsonIgnore's @Target not including ElementType.PARAMETER is only one-half of the problem.
  2. prop.addCtor(..., false) <--- this hardcoded false in POJOPropertiesCollector is the other half of the problem.

Sorry!

yihtserns avatar Jul 22 '24 02:07 yihtserns

Uggh. As little as I like the idea, maybe I should revert this change. WDYT @yihtserns .

But at least it was possible to pinpoint this change as the root cause for failing tests.

cowtowncoder avatar Jul 22 '24 21:07 cowtowncoder

I think it's fine to revert at the first sign of problem - I believe you have more important things to focus on than this (+ to reduce any unnecessary risk for the upcoming 2 18 release).

And it's not like this is something urgent or requested, we can always do it later if we really want to.

yihtserns avatar Jul 22 '24 21:07 yihtserns

PR reverted.

cowtowncoder avatar Jul 22 '24 21:07 cowtowncoder

I myself was going to open a request for this feature cuz it is very useful specially when using lombok to have @AllArgsConstructor

Or at least have an Ignore unkwon properties that really work. ex

@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class C{ 
   @JsonIgnore 
   private Board board;

      @JsonProperty(value = "title")
    private String title;


     @JsonProperty(value = "position")
    private int position;
}

then if i try

objectMapper.readValue("{\"title\":\"title\",\"position\":2}",C.class);

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `C`: Argument #0 of constructor [constructor for `C` (3 args), annotations: [null] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
 at [Source: (String)"{"title":"title","position":2}"; line: 1, column: 1]

as far as i know there is no way to solve this without creating a custom deserializer or manually creating a constructor with @JsonCreator and both options are really inconvenient...

would be appreciated have an way to solve it with aspects or at least in the configuration of ObjectMapper

bolds07 avatar Aug 15 '24 19:08 bolds07

Unfortunately not sure we can proceed with this.

cowtowncoder avatar Aug 15 '24 19:08 cowtowncoder

@bolds07 your example would work with the addition of @NoArgsConstructor. The problem here is not due to ignored properties, but constructor signature.

deshack avatar Sep 16 '25 08:09 deshack