jackson-datatype-hibernate icon indicating copy to clipboard operation
jackson-datatype-hibernate copied to clipboard

Lazy associations mapped as null values even if JsonInclude.NON_NULL is present

Open viniciusccarvalho opened this issue 13 years ago • 15 comments

This module rocks! But I think we are missing a feature to actually not include the association at all in the response. I have the module setup and my mapper is using mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

But all the lazy associations are displayed in my json as null values. The other nulls I have (single table columns) are not serialized but the associations are displayed.

viniciusccarvalho avatar Oct 02 '12 22:10 viniciusccarvalho

This is tricky one unfortunately; the problem being that handling of null values occurs by the serializer itself; which is unfortunately too late for null suppression. And conversely caller does not know that value should be treated as null.

cowtowncoder avatar Jan 05 '13 04:01 cowtowncoder

Is it possible to add a new Feature in HibernateModule that overrides the writing of null and does not write the lazy associations?

ralscha avatar Apr 08 '13 06:04 ralscha

That is not easy, since each individual serializer would need to handle null suppression or masking, so there is no general facility for doing that. It might be easier for just suppressing lazy references.

But in general what would help most is finding active developers for this module -- I try to help, but I don't use Hibernate myself, so I don't have as much itch as others would.

cowtowncoder avatar Apr 08 '13 17:04 cowtowncoder

+1

peruzzo avatar Oct 17 '13 13:10 peruzzo

+1

vijayrawatsan avatar Nov 09 '13 20:11 vijayrawatsan

Code contributions are most welcome!

It would be very nice if we had a unit test to reproduce this -- perhaps something based on a real project. Fixing might be easy.

One other comment: instead of using NON_NULL, one that is more likely to work is NON_EMPTY -- it will be easier to make work due to way proxies are handled.

cowtowncoder avatar Nov 09 '13 20:11 cowtowncoder

I added proper handling of @JsonInclude(Include.NON_EMPTY) for lazy-loaded collections; whether same can be made to work for NON_NULL is unclear because physical property value is not null ever (there is a proxy for handling lazy-loading). So I would strongly recommend using Include.NON_EMPTY setting instead.

cowtowncoder avatar Nov 09 '13 21:11 cowtowncoder

+1

sammy-tam avatar Aug 12 '15 00:08 sammy-tam

Hi guys, I am interested in this issue. Is somebody working on this? I can provide a real example to reproduce the issue.

mmonti avatar Feb 03 '16 08:02 mmonti

@mmonti I don't think anyone is working on this. A reproduction would be helpful; especially if it demonstrates the issue on Jackson 2.7.

cowtowncoder avatar Feb 04 '16 02:02 cowtowncoder

@cowtowncoder I put together an example of the issue #21 in this project: https://github.com/mmonti/entity-graph

There is just one test: ApplicationTests.testIdentityLazyManyToMany() which is self explanatory. The hibernate module is configured in the class WebConfig.java

Let me know if you have any questions about the example and also how can I help with this.

mmonti avatar Feb 04 '16 09:02 mmonti

Does this issue still exist? Its really critical because directly affects client side, Any new news?

n0ise9914 avatar Jun 08 '18 14:06 n0ise9914

It's working very well (removing null/empty fields), please close the issue. mapper.serializationInclusion(JsonInclude.Include.NON_EMPTY);

Hibernate: 5.2.16.Final Jackson: 2.5.9 Jackson-datatype-hibernate5: 2.9.5

n0ise9914 avatar Jun 10 '18 08:06 n0ise9914

@n0ise9914 It's about JsonInclude.NON_NULL, so the issue is still present. Not everyone wants to switch to Include.NON_EMPTY, as this is a different option.

mattiashtd avatar Mar 30 '19 15:03 mattiashtd

this is still a (big) problem because it does not work on version 2.9.8 Any body working on that ?

gpeel avatar May 02 '19 11:05 gpeel