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

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

Open
cowtowncoder opened this issue Jul 21, 2024 · 7 comments

Comments

@cowtowncoder
Copy link
Member

(note: off-shoot of FasterXML/jackson-databind#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.

@yihtserns
Copy link

yihtserns commented Jul 22, 2024

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
Copy link

yihtserns commented Jul 22, 2024

Ugh, I missed out an important fact in FasterXML/jackson-databind#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!

@cowtowncoder
Copy link
Member Author

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.

@yihtserns
Copy link

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.

cowtowncoder added a commit that referenced this issue Jul 22, 2024
@cowtowncoder cowtowncoder removed this from the 2.18.0 milestone Jul 22, 2024
@cowtowncoder
Copy link
Member Author

PR reverted.

cowtowncoder added a commit that referenced this issue Jul 22, 2024
@cowtowncoder cowtowncoder reopened this Jul 23, 2024
@cowtowncoder cowtowncoder removed the 2.18 label Jul 23, 2024
@bolds07
Copy link

bolds07 commented Aug 15, 2024

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

@cowtowncoder
Copy link
Member Author

Unfortunately not sure we can proceed with this.

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

No branches or pull requests

3 participants