jackson-annotations
jackson-annotations copied to clipboard
Add `ElementType.PARAMETER` to `@JsonIgnore` to allow use for Constructor parameters
(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.
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:
@JsonIgnoreadded on the generated field. - After this change:
@JsonIgnoreadded 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
@Targetannotation of the annotation being used. If there are multiple applicable targets, the first applicable target from the following list is used:
- param
- property
- field
Ugh, I missed out an important fact in https://github.com/FasterXML/jackson-databind/issues/4626:
@JsonIgnore's@Targetnot includingElementType.PARAMETERis only one-half of the problem.prop.addCtor(..., false)<--- this hardcodedfalseinPOJOPropertiesCollectoris the other half of the problem.
Sorry!
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.
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.
PR reverted.
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
Unfortunately not sure we can proceed with this.
@bolds07 your example would work with the addition of @NoArgsConstructor.
The problem here is not due to ignored properties, but constructor signature.