micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Overriding ObjectMapperFactory does not work for setDefaultSetterInfo

Open StefanSrf opened this issue 1 year ago • 3 comments

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

  1. subclass ObjectMapperFactory
  2. use super.objectMapper(jacksonConfiguration, jsonFactory).setDefaultSetterInfo( JsonSetter.Value.construct(Nulls.DEFAULT, Nulls.SKIP )) to override the config
  3. use kapt("io.micronaut:micronaut-http-validation")
  4. 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

StefanSrf avatar Dec 22 '23 11:12 StefanSrf

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 avatar Dec 22 '23 15:12 graemerocher

@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.

StefanSrf avatar Jan 05 '24 07:01 StefanSrf

We currently can't use the management endpoint for loggers. Are there plans to investigate in and probably solve this issue?

ideadapt avatar Mar 26 '24 09:03 ideadapt