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

support disallow list for default typing #4182

Open
pjfanning opened this issue Oct 28, 2023 · 3 comments
Open

support disallow list for default typing #4182

pjfanning opened this issue Oct 28, 2023 · 3 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@pjfanning
Copy link
Member

pjfanning commented Oct 28, 2023

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

The idea is that when mapper activateDefaultTyping is enabled that not every class name would be written into the serialized JSON.
For me, this default typing feature is useful when you have a class like record MyRecord(int id, MyInterface interface) where MyInterface is a basic interface (or sealed class) and there are a few possible subclasses of MyInterface.

In FasterXML/jackson-module-scala#643, we don't really want a Map implementation class name to be written. jackson-module-scala doesn't need help with filling in a Scala Map. In fact, the class name in the serialized JSON persuades jackson-databind to try to build the Map instead of letting jackson-module-scala build it.

What I am suggesting is that when activateDefaultTyping is used, that all classes are affected (as happens now) but that we add a new method that lets users block this from happening for certain classes. It would be nice if this supported package names as well as class names. So the input would be a String or List of Strings but the individual String could be the package name and we could do a startsWith check.

Describe the solution you'd like

Serialized JSON would not include class names where they matched a disallowed class/package name. Ideally deserialization would also ignore any classes that matched a disallowed class/package name.

something like:
void defaultTypingIgnores(String... names)

Usage example

No response

Additional context

No response

@pjfanning pjfanning added the to-evaluate Issue that has been received but not yet evaluated label Oct 28, 2023
@JooHyukKim
Copy link
Member

JooHyukKim commented Oct 29, 2023

Perhaps we could consider adopting a fluent style, such as:

   .activateDefaultTyping(...)
   .except(Package.withNameMatching("com.jackson.some"))

This approach might offer more flexibility, especially if we anticipate more matching mechanisms in the future.

However, the tradeoff from a development perspective is that it's more verbose and would require much more additional effort.

@cowtowncoder
Copy link
Member

We already have DefaultTyping enumerations for determining when Type Id is to be included. So maybe something could be added to allow similar extension point for users.
I am not too excited about supporting List of class names of type to exclude since that won't scale very well.

@marilatte53
Copy link

I am currently facing a similar issue, since I want to deserialize JSON into a POJO containing nested classes with interface properties. My idea is to let users create a POJOs via a JSON file, however I only want specific interfaces (that are dynamically added to a list) to be resolved via type ids. For example instead of List I want to always use ArrayList and ignore any possible type ids.
I think this would be possible, if the AsArrayTypeDeserializer supported default implementations, but the responsible code is commented out:

// 11-Nov-2020, tatu: I don't think this branch ever gets executed by
//    unit tests so do not think it would actually work; commented out
//    in 2.12.0
/* if (_defaultImpl != null) {
        p.nextToken();
        return _idResolver.idFromBaseType();
    }
*/

Maybe what I'm doing is completely wrong and there is a better solution. Alternatively, I could probably use a custom Deserializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

4 participants