jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Enable turning off look-up optimisation in TypeFactory

Open CarstenEnglert opened this issue 7 years ago • 5 comments

While building a library for generating JSON Schemas (Draft 7) from Java types (which is not limited to JSON but can also be used to document the Java type itself in the form of a JSON Schema), I ended up "re-inventing the wheel" for the whole type resolution topic, which works fine but is a whole new API for something that already exists here in jackson-databind.

Resolution: I'm throwing away my own type resolution implementation in favour of the jackson JavaType, TypeFactory etc. However, some limitations (such as ignoring static fields and methods) are hard-coded in final classes such as AnnotatedFieldCollector and AnnotatedMethodCollector, which I had to copy-paste and then adjust instead of being able to simply create a sub-type and override accordingly. While those two classes were simple/small enough for that kind of approach (even though ugly), doing the same for the TypeFactory is impractical (where Enum and Comparable are "optimised" in a way, that their self-references are being ignored – which I want to have in order to properly resolve the parameter and return types of respective methods).

protected JavaType _fromParamType(ClassStack context, ParameterizedType ptype,
        TypeBindings parentBindings)
{
    // Assumption here is we'll always get Class, not one of other Types
    Class<?> rawType = (Class<?>) ptype.getRawType();

    // 29-Oct-2015, tatu: For performance reasons, let's streamline handling of
    //   couple of not-so-useful parametric types
    if (rawType == CLS_ENUM) {
        return CORE_TYPE_ENUM;
    }
    if (rawType == CLS_COMPARABLE) {
        return CORE_TYPE_COMPARABLE;
    }

Long story short: my use-case is a special one that arises from using this type resolution logic outside of the limitations of JSON, i.e. I don't expect jackson-databind to support those directly, but removing the final modifiers from (at least) the TypeFactory class would go a long way in making all that re-usable. The affected methods are already protected (oddly enough in a final class). An alternative way of removing the unwanted optimisation would, of course, be appreciated as well.

I'm currently basing this on jackson(-databind) version 2.9.9.


I can appreciate that Joshua Bloch's recommendation in "Effective Java" is being followed here ("Design and document for inheritance or else prohibit it") and looking at the code of the TypeFactory it really does not look like it was designed for inheritance (apart from the existence of numerous protected methods).

With that in mind, adding a (by default enabled) option for including those "optimisations" would be my preferred choice, but introducing this (albeit small) additional complexity for my non-JSON use-case seems wrong.

What do you reckon?

  1. Simply remove the final keyword on this one class,
  2. introduce an interface implemented by the standard TypeFactory which can be implemented by an alternate wrapper/proxy, and that is used throughout various places instead of the specific class,
  3. add a setting for turning the Enum/Comparable optimisation on/off?

CarstenEnglert avatar May 29 '19 21:05 CarstenEnglert

Ok, first one quick suggestion: depending on exactly what parts you find useful, you might want to have a look at ClassMate library instead:

https://github.com/FasterXML/java-classmate

because it handles generic type resolution very well (I wrote it based on findings from Jackson TypeFactory). It does not have POJO property inference pieces, but core type resolution for types, subtypes, fields and methods is rock solid. It also lacks any Jackson-use-case-specific business logic.

cowtowncoder avatar May 30 '19 22:05 cowtowncoder

Thanks for the suggestion @cowtowncoder – a pity I already spent the time making my alternative work as well. Since your API looks more generic (even if a bit more complicated) than mine, I'll give that a go instead. I'd rather benefit from your experience than having to maintain that kind of logic myself. 😉

I was only asking this here on jackson-databind as I already have that as a dependency in order to construct the generated JSON Schema in the form of an ObjectNode – just means I have one more dependency.

Out of curiosity: why not use ClassMate in jackson? Backwards compatibility I presume; or because you want to preserve the performance optimisations?

CarstenEnglert avatar Jun 02 '19 21:06 CarstenEnglert

Good questions! So, originally I started ClassMate to solve issues jackson-databinds TypeFactory could not -- which it does -- but part of this was that I felt TypeFactory itself could not be retrofitted with correct logic, nor Jackson made to use ClassMate. Big problem being that JavaType is sort of tightly couple with some aspects of Jackson's business logic (like exposing MapType and CollectionType as very distinct).

Over time I did end up using parts of ClassMate for jackson-jr, actually, and eventually I did rewrite TypeFactory (for 2.7 I think?) -- something I had thought I'd never do, due to risks. And for that I also used things I learned from writing ClassMate, so code is bit more similar.

So yes, big part of that is just that retrofit would be quite difficult. And after rewrite, Jackson's type resolution is actually in bit better shape. Still not exactly as good as ClassMate (there are some warts in places with deeply nested subtypes of MapType/CollectionType, and probably for type refinements needed for CollectionLikeType/MapLiketype), but quite close.

Performance could be another potential concern but I don't think that's as clear cut: type resolution is quite costly but it is hard to know which one is actually less inefficient without measurements.

Oh and as to using TypeFactory: completely understandable, compared to starting from scratch. Some other libraries/frameworks use it too, in a way: I think Swagger tools do that (although with bit different route, using "schema generation" callbacks from ObjectMapper). It's just tricky due to tight coupling with some parts of Jackson's databinding logic, needs.

cowtowncoder avatar Jun 03 '19 18:06 cowtowncoder

Alright, I finished the refactoring of my library to use classmate instead of my own (or the jackson-databind) type resolution logic.

I.e. I have no need for the changes mentioned here anymore, I'm therefore closing this issue.

Just in case you are interested: my JSON Schema (Draft 7) generation library can be found here: https://github.com/victools/jsonschema-generator The version v1.0.x was using my own type resolution logic (which also worked at least for the use-cases I had tested). The new version v2.0.0 now uses classmate instead.

CarstenEnglert avatar Jun 06 '19 22:06 CarstenEnglert

@CarstenWickner excellent!

I'll create another issue so I remember to add a link to this tool, as a replacement for Jackson's obsolete JSON schema module.

cowtowncoder avatar Jun 07 '19 18:06 cowtowncoder