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

@JsonIgnoreProperties not working with @JsonValue #3647

Closed
ThatSneakyRaccoon opened this issue Oct 31, 2022 · 7 comments
Closed

@JsonIgnoreProperties not working with @JsonValue #3647

ThatSneakyRaccoon opened this issue Oct 31, 2022 · 7 comments
Labels
2.16 Issues planned for 2.16 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Milestone

Comments

@ThatSneakyRaccoon
Copy link

When serializing the class Bar using the default ObjectMapper

class Foo {
     
     String p1 = "hello";
     String p2 = "world";
     
}
class Bar {

     @JsonValue
     @JsonIgnoreProperties( "p1" )
     Foo foo = new Foo();
    
}

The output is

{
  "p1" : "hello",
  "p2" : "world"
}

I would expect it to be

{
  "p2" : "world"
}

Tested on version 2.13.1

@ThatSneakyRaccoon ThatSneakyRaccoon added the to-evaluate Issue that has been received but not yet evaluated label Oct 31, 2022
@cowtowncoder
Copy link
Member

Correct: this is a limitation and combination does not work.

I will leave this open as possible future enhancement if anyone has time and interest: I agree that it would make sense to support. But there is no on-going work in this area that I know of.

@cowtowncoder cowtowncoder changed the title @JsonIgnoreProperties not working with @JsonValue @JsonIgnoreProperties not working with @JsonValue Nov 1, 2022
@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Nov 1, 2022
@mukham12
Copy link
Contributor

mukham12 commented Apr 2, 2023

Hi @cowtowncoder.

I'm currently looking into it, but I'm still familiarizing myself with the codebase and haven't found the best approach yet. Obviously, fields marked with both @JsonValue and @JsonIgnoreProperties separately and together require different code flows, so I could not find an overlapping method that could be used to deal with the combo.

If you have any suggestions on where I should focus that could speed up development, I'd love to hear them! I'm open to feedback and appreciate any guidance you can provide.

One of the challenges I'm facing is that, when serializing individual fields like p1 and p2, we don't have a way to know if the class they are in (e.g. Foo) is marked with @JsonIgnoreProperties inside of Bar. This makes it more difficult to determine the correct behavior for each field. Or maybe I am just looking in the wrong place :)

Looking forward to hearing from you.

@cowtowncoder
Copy link
Member

@mukham12 I don't remember off-hand exactly where to look (if this was simple to do I might have worked on it earlier :) ).

But there are 2 obvious things to try to look for (apologies if this is too obvious):

  1. Handling of JsonValueSerializer (src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java) -- how it get constructed, configured etc
  2. Handling of property-defined @JsonIgnoreProperties, how it propagated to POJOs (BeanSerializer, BeanSerializerBase) -- this is different from (but combined with) annotation as used for class itself

the trick, basically, would be to combine 2 things, to make JsonValueSerializer somehow pass configuration/contextualization of "ignorable property names" to BeanSerializer, but ideally in a way that does not hard-code type (but perhaps it has to; they are ways around this like tag-interfaces).

@cowtowncoder
Copy link
Member

Also, one other minor note: writing a (failing) unit test, to be added under src/test/java/com/fasterxml/jackson/failing would be useful as well; this is often the starting point for implementations.

@mukham12
Copy link
Contributor

mukham12 commented Apr 4, 2023

Thanks a lot for the suggestions. I will keep looking into this and I am going to keep you posted.

mukham12 added a commit to mukham12/jackson-databind that referenced this issue Apr 18, 2023
cowtowncoder pushed a commit that referenced this issue Apr 18, 2023
@cowtowncoder cowtowncoder added has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue 2.16 Issues planned for 2.16 labels Apr 18, 2023
@JooHyukKim
Copy link
Member

JooHyukKim commented Aug 5, 2023

I wrote a sample code that partly solves this issue #4070. The PR might do the job.

@cowtowncoder cowtowncoder added this to the 2.16.0 milestone Oct 1, 2023
cowtowncoder added a commit that referenced this issue Oct 1, 2023
JooHyukKim added a commit to JooHyukKim/jackson-databind that referenced this issue Oct 1, 2023
@cowtowncoder
Copy link
Member

Forgot to close; included in 2.16.0-rc1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.16 Issues planned for 2.16 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Projects
None yet
Development

No branches or pull requests

4 participants