micronaut-core
micronaut-core copied to clipboard
Overriding ObjectMapperFactory does not work for setDefaultSetterInfo
Expected Behavior
Subclassing ObjectMapperFactory and configuring the mapper should work when using kapt. For example to set contentNulls to Nulls.SKIP to avoid having null values in lists which are declared to have only non-null entries.
` val mapper = super.objectMapper(jacksonConfiguration, jsonFactory)
@Suppress("CommentWrapping")
mapper.setDefaultSetterInfo(
JsonSetter.Value.construct(
/* nulls = */ Nulls.DEFAULT,
/* contentNulls = */ Nulls.SKIP
)
)
`
Actual Behaviour
The configuration for contentNulls = Nulls.SKIP is ignored. This results in runtime exceptions.
Using an ObjectMapperFactory that does not subclass ObjectMapperFactory prevents the bug, that's our current workround. However, this breaks other functionality, for exampling POST requests to the built-in loggers endpoint result in micronaut runtime exceptions.
Steps To Reproduce
- subclass ObjectMapperFactory
- use super.objectMapper(jacksonConfiguration, jsonFactory).setDefaultSetterInfo( JsonSetter.Value.construct(Nulls.DEFAULT, Nulls.SKIP )) to override the config
- use kapt("io.micronaut:micronaut-http-validation")
- Post a body that contains null values in the list.
See example project
Environment Information
Ubuntu
Example Application
https://github.com/StefanSrf/micronaut-jackson-override-bug
Version
4.2.2 but probably since 4.0.0
I believe you have to use @Replaces(factory=.. bean = ..)
However I would recommend for this use case to instead write a BeanCreatedEventListener<ObjectMapper>
and just modify the bean on creation since it doesn't look like a replacement factory is needed
@graemerocher thanks for the hint. The BeanCreatedEventListener approach is more elegant. Unfortunately the problem remains the same. I updated the example application. null values still appear in non-null-lists.
We currently can't use the management endpoint for loggers. Are there plans to investigate in and probably solve this issue?