Skip to content

Conversation

@nsoft
Copy link
Contributor

@nsoft nsoft commented Jun 29, 2025

In this PR I have added partial support for default typing in NON_FINAL and EVERYTHING modes.

This support is not perfect because the Jackson API is not making it easy for a serializer to discern exactly what the default typing mode, and class identifier property are. Default typing is enabled on the mapper directly and does not set feature flags that can be interrogated, nor was I able to find a means to heuristically determine such information. There wasn't even any obvious way to leverage reflection to dig out this information. This is quite disappointing because it does seem that this is something a serializer needs to take into account to produce legal output. It might be worth discussing this with the Jackson project to see if I've missed something or if there is a good way for them to improve this in future versions. I did a quick look on more recent versions and didn't find anything. I mostly focused on the version of Jackson currently specified by this project (2.10.3), since changing compatibility with versions is an additional burden, and probably should be done in it's own PR.

Nonetheless, by specifying an attribute visible in the mapping context for the writer, I am able to output class information.

What is supported in this PR:

  • DefaultTyping.NON_FINAL
  • DefaultTyping.EVERYTHING (this is deprecated by jackson, but central to the issue report)
  • Fully qualified class names for type identification (i.e. "com.example.MySerializedClass")

What is no supported:

  • Other modes such as JAVA_LANG_OBJECT, OBJECT_AND_NON_CONCRETE, NON_CONCRETE_AND_ARRAYS
    • These modes seem to get caught in some sort of catch 22 where @class is not expected in some cases and required in others. No exception is thrown if we add .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) but the output is not correct and some objects become duplicated, so I have aggresively documented against this combination.
  • Short class names (i.e. ".MySerializedClass")
    • I experimented with passing in mode information and this information via additional attributes, and it's possible, but my difficulties with alternate modes left me disinclined to add the complexity for this at this time.

All unit tests pass, and I added a new test specific to Issue26 to document it and it's limitations README file was updated to provide guidance on how to enable this support.

During this investigation I also had some ideas on how to optimize the deserializer to avoid needless creation of an immediately garbage collectable JsonNode object and simplify the code for this feature slightly too, but that can be a follow on PR.

nsoft added 9 commits June 26, 2025 21:47
add the type information to JSOGRef as shown in Issue26Test.java
to not add it in the event that default typing is NOT on. Jackson
doesn't seem to provide any means for serializers to know if default
typing is turned on or not. This is amounting to a lot of code
that tries to not touch the original (nifty) deserialize method
using JsonNode, but it might be simpler to change that since it is
fundamentally treating a reference as a full node and Jackson isn't
expecting that AFAICT.
what the applicable attribute name might be, but it looks like we can at
least set this as a serialization provider attribute. Ideally we would
just query the jackson internals, but at least this can be configured
at the time we create the mapper
@nsoft
Copy link
Contributor Author

nsoft commented Jun 29, 2025

Hmm didn't seem to link #26 so mentioning it in a comment.

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

Successfully merging this pull request may close these issues.

1 participant