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

Avoid load of not requested lazy properties #6

Open
heruan opened this issue Jan 29, 2016 · 4 comments
Open

Avoid load of not requested lazy properties #6

heruan opened this issue Jan 29, 2016 · 4 comments

Comments

@heruan
Copy link

heruan commented Jan 29, 2016

On a JPA project, when a POJO has a List<Bean> property which should be lazy-loaded, the loading gets triggered even if I'm not requesting the property in the filter. It would be great to instruct Jackson not to call the getter of these properties to avoid unnecessary loading!

@jhaber
Copy link
Member

jhaber commented Jan 29, 2016

Hey Giovanni, this is hard to avoid with the current design. Our message body writer converts the response to a JsonNode, filters the JsonNode based on the requested properties, and then writes the filtered JsonNode to the response stream.

A different approach would be to use Jackson's native PropertyFilter; I can play around with this and see if it's feasible. My first concern is that PropertyFilter might only work for bean properties using the standard bean serializer, whereas the current design lets you filter maps, objects with custom serializers, etc.

@jhaber
Copy link
Member

jhaber commented Jan 29, 2016

I played around with this for a little bit but ran into a few issues. PropertyFilter does work for maps, but not for objects with custom serializers. Also, it seems that only one PropertyFilter can be active for a given object, meaning that activating our PropertyFilter would cause any existing PropertyFilter registered for the object to not work. Finally, inside the PropertyFilter I don't see any way to access the full path to the property name which is necessary for filtering on subfields using dot-notation (ie, if you had JSON similar to the one in the readme, when filtering the child subobject the fields would just be called id and name, there would be no way for the filter to tell that it was inside of subobject called child).

Let me know if you have any other ideas, otherwise I don't think there's much we can do to solve this use-case unfortunately

@heruan
Copy link
Author

heruan commented Feb 29, 2016

Hi @jhaber and thank for your interest in this issue. I'm sure this is doable with Jackson's PropertyFilter including nested fields with dot-notation, since it's what Jersey's Entity Filtering does; see also [1] and [2].

It supports runtime filtering by query string parameter, filtering by security constraints and custom annotation based filters; and all before Jackson serialization, thus preventing access to lazy loaded properties.

[1] https://github.com/jersey/…/org/glassfish/jersey/message/filtering
[2] https://github.com/jersey/…/jackson/internal/JacksonFilteringFeature.java

@nickshoe
Copy link

nickshoe commented Sep 4, 2019

If you need contextual serialization then you could use this: https://www.baeldung.com/jackson-json-view-annotation

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