Skip to content

Cannot access attributes from Converter #4915

@jakub-bochenski

Description

@jakub-bochenski

Is your feature request related to a problem? Please describe.

I tried this, but the result is cached, meaning that the attribute value is always the same:

.setHandlerInstantiator(object : HandlerInstantiatorAdapter() {

                override fun converterInstance(
                    config: MapperConfig<*>,
                    annotated: Annotated,
                    implClass: Class<*>
                ): Converter<*, *> {
                    if (implClass == MyConverter::class.java) {
                        return MyConverter {
                            config.attributes.getAttribute("test") as String
                        }
                    }
                    return implClass.newInstance() as Converter<*, *>
                }
            })

Describe the solution you'd like

Ideally Converter would have access to the MapperConfig in the convert() method.

Maybe there could be a new interface to opt-in for this, since the signature change would be breaking.
(E.g. like ResolvableSerializer, but it would have to be called per-usage to work around caching).

Caching serializes on a per-config basis would allow the above code to work correctly.

Usage example

mapper.writer().withAttribute("myAttr", "Hello!").writeValueAsString(something);

Additional context

Currently you have to rely on ThreadLocal or write a full custom Serializer to have access to context values. It would be nice if you could use attributes in a Converter since it's much simpler to implement than a Serializer

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.19Issues planned at 2.19 or later

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions