Skip to content

[REQ] [JavaSpring] use JsonTypeInfo.Id.DEDUCTION for oneOf interface #20891

@jpfinne

Description

@jpfinne

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

When using useOneOfInterfaces=true, the generated code has an interface and an implementation for each options.
This allows serialization from Java to Json.

It does not allow deserialization from json to Java because Jackson does not know which class to instantiate.

Describe the solution you'd like

Add an option to allow the addition of @JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION) to the interface

On the generated interface, @jSonTypeInfo with id Deduction and @JSonSubTypes are generated with all the classes implementing the interface when

  • oneOfInterfaces=true
  • oneOfInterfacesDeduction=true
  • no discriminator mapping defined

Describe alternatives you've considered

use useOneOfInterfaces=false (it generates a class composed of all the individual fields)
use x-field-extra-annotation annotation or custom annotation like in #10993

Additional context

@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
@JsonSubTypes({
    @JsonSubTypes.Type(value = Dog.class), 
    @JsonSubTypes.Type(value = Cat.class)
})

public interface Animal {
}

can be deserialized by Jackson

 @Test
    void jsonTypeInfoDeduction() throws JsonProcessingException {

        JsonMapper jsonMapper = JsonMapper.builder().build();
        var object = jsonMapper
                .readValue("""
                        {
                           "bark": true 
                        }
                        """, Animal.class);

        System.out.println(object);

        object = jsonMapper
                .readValue("""
                        { 
                         "declawed": "true"
                        }
                        """, Animal.class);

        System.out.println(object);
    }

show that the json can deserialize into the correct class:

class Dog {
    bark: true
}
class Cat {
    declawed: true
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions