-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
BeanDeserializerModifier not called again for changed config #1956
Comments
@simontb Not a bug, feature. All serializers/deserializers are created once and cached. Modifiers are only called during creation. View application, on the other hand, is dynamic, and you can not rely on specific view settings in modifier. So this approach will unfortunately not work. |
Thanks for the quick reply. I already figured out, that this is intended in the current implementation, but what's the point in having access to |
But to change active view you will need to use I hope this helps. |
Really late reply, but I had the same challenge. And also from a Spring (Boot 3) application. I noticed that the MappingJackson2HttpMessageConverter contains a @Component
public class ActiveViewAwareMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
private final Map<Class<?>, ObjectMapper> activeViewAwareObjectMappers = new HashMap<>();
public ActiveViewAwareMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper);
}
@Override
protected ObjectReader customizeReader(ObjectReader reader, JavaType javaType) {
var activeView = reader.getConfig().getActiveView();
if (activeView != null) {
var customViewObjectMapper = activeViewAwareObjectMappers.computeIfAbsent(activeView, view -> {
var module = new SimpleModule();
module.setDeserializerModifier(new IgnoreJsonViewBeanDeserializerModifier(view));
return getObjectMapper().copy().registerModule(module);
});
return customViewObjectMapper.readerWithView(activeView).forType(javaType);
}
return super.customizeReader(reader, javaType);
}
@RequiredArgsConstructor
private static class IgnoreJsonViewBeanDeserializerModifier extends BeanDeserializerModifier {
private final Class<?> view;
@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config,
BeanDescription beanDesc,
BeanDeserializerBuilder builder) {
var propertyIterator = builder.getProperties();
while (propertyIterator.hasNext()) {
final var property = propertyIterator.next();
if (!property.visibleInView(view)) {
builder.addIgnorable(property.getName());
}
}
return builder;
}
}
} |
To work around #437 I wrote a
BeanDeserializerModifier
and override the updateBuilder method. There I retrieve the active view by callingconfig.getActiveView()
and remove the properties that are not visible from the builder.When I use the same
ObjectMapper
to read my model with different views, the DeserializerModifier is only called for the first read and therefore does not adjust the properties.This is my model:
This is the code reading the model:
I would expect the read with the view
Hidden.class
to fail, because a is provided instead of b, but the output is:The text was updated successfully, but these errors were encountered: