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

Custom SimpleModule not included in list returned by ObjectMapper.getRegisteredModuleIds() after registration #3110

Closed
dkindler opened this issue Apr 10, 2021 · 4 comments
Milestone

Comments

@dkindler
Copy link

Describe the bug
I'm trying to register a SimpleModule that simple uses some custom serializers and deserializers I need. Based on the output of mapper.registeredModuleIds, it doesn't appear that the module is being registered to the mapper.

Version information
2.12.2

To Reproduce

    val mod = SimpleModule()
        .addSerializer(DBDecimal::class.java, DBDecimalSerializer())
        .addDeserializer(DBDecimal::class.java, DBDecimalDeserializer())

    val mapper = jacksonObjectMapper()
        .setTimeZone(TimeZone.getTimeZone("UTC"))
        .registerModule(JavaTimeModule())
        .registerModule(mod)
        .registerModule(MoneyModule())
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false).also {
            println("!!!!!!!!!!!!!!!")
            println(it.registeredModuleIds)
        }
[com.fasterxml.jackson.module.kotlin.KotlinModule, com.fasterxml.jackson.datatype.jsr310.JavaTimeModule, org.zalando.jackson.datatype.money.MoneyModule]

Expected behavior
The SimpleModule is registered and appears in the output of registeredModuleIds

@dkindler dkindler added the to-evaluate Issue that has been received but not yet evaluated label Apr 10, 2021
@cowtowncoder cowtowncoder added 2.13 and removed to-evaluate Issue that has been received but not yet evaluated labels Apr 10, 2021
@cowtowncoder
Copy link
Member

Hmmh. This is somewhat interesting.

First of all, I think that SimpleModule actually is registered, despite id not showing.
As to why id is not included, things get bit more complicated: this is a combination of two different things.

First, there is feature MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS (enabled by default) that is to prevent duplicate registration of a module. Since it is unlikely to be useful for SimpleModule, it is designed in a way so as not to apply to basic SimpleModule (but to apply to sub-classes).
This, in turn, is implemented by SimpleModule returning null from getTypeId() (unless sub-classed).
End result then is that no type id is included in list of registered module type ids returned by getRegisteredModuleIds().

I will add a note on Javadoc of getRegisteredModuleIds() to mention this, but am not quite sure what else could be done -- there is the challenge of avoiding unintended prevention of registering multiple instances.
One minor improvement could be to make sure that if user specifies explicit name in constructor, that would be used as non-null "type id". I'll consider this issue to be about doing just that, but I am open to other ideas as well.

In your case you can sub-class SimpleModule if you want, and this should give it distinct type id that would get included.

@cowtowncoder cowtowncoder changed the title Jackson doesn't appear to be registering custom module Custom SimpleModule not included in list returned by ObjectMapper.getRegisteredModuleIds() after registration Apr 10, 2021
@dkindler
Copy link
Author

Thanks @cowtowncoder -- Your suggestions did in fact work

cowtowncoder added a commit that referenced this issue Jun 7, 2021
@cowtowncoder cowtowncoder added this to the 2.13.0 milestone Jun 7, 2021
@cowtowncoder
Copy link
Member

Changed it so that if (and only if) code specifies explicit name for SimpleModule, that will be used as the module "type id", and consequently that id will be returned as expected.

cowtowncoder referenced this issue in FasterXML/jackson-module-kotlin Jun 15, 2021
cowtowncoder added a commit that referenced this issue Jun 16, 2021
@cowtowncoder
Copy link
Member

Ended up rewriting some aspects and now ALL modules should actually be listed, including "anonymous" SimpleModules. It should not lead to duplicate registrations either (added some basic tests to sanity check).

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

2 participants