FasterXML / java-classmate

Library for introspecting generic type information of types, member/static methods, fields. Especially useful for POJO/Bean introspection.
http://fasterxml.com
Apache License 2.0
257 stars 42 forks source link

How to build AnnotationOverrides from ObjectMapper #81

Open CarstenWickner opened 9 months ago

CarstenWickner commented 9 months ago

Hi Tatu,

In the context of my JSON Schema Generator library, I've been asked to support the mix-ins registered on a given Jackson ObjectMapper, when resolving members via classmate. I saw the usage of the AnnotationOverrides type in classmate expecting this method to be implemented:

List<Class<?>> mixInsFor(ClassKey);

Unfortunately, the ClassKey is a utility type, which doesn't offer a getter for the wrapped Class, which complicates things.

Likewise on the Jackson ObjectMapper, there is no way to retrieve a full set of registered mix-ins as a simple Map. Just this method:

Class<?> findMixInClassFor(Class<?>)

Any suggestions on how to implement the AnnotationOverrides based on a fully configured ObjectMapper without requiring the same mix-ins to be provided all over again?

cowtowncoder commented 9 months ago

@CarstenWickner I am not sure I have a good answer at this point; some access does seem to be missing.

On possible changes: addition of accessor for raw class for ClassKey would be perfectly acceptable -- both for ClassMate and Jackson. From practical perspective it's probably quicker to get a version of ClassMate released with such change due to much longer release cycle of Jackson.

But the real challenge is the ability (or lack thereof) to find all mix-ins registered: Jackson's MixInResolver leaves out bulk accessor on purpose: this allows resolver to dynamically figure things out, so that definition is not necessarily simple mapping from Class to Class... although the default implementation does implement it that way.

I guess one possibility would be to add optional accessor for this information; where implementations would be free to throw UnsupportedOperationException is implementation does not (can not) provide such mapping. Mapping itself should be provided as a callback of some sort, not returning Map but allowing iteration over entries. I would accept a PR for doing that.

But also... I am not sure if MixInResolver itself is yet accessible: if not, getter for that would be acceptable addition as well.

All of which is to say that quite a bit of work would be needed so I don't have a quick solution. But would be happy to work in providing some access if you have time to contribute.