Skip to content

[REQ] Option for specifying Default implementation class in jackson polymorphic class #18680

Open
@CSALS

Description

@CSALS

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

  • In java, while generating polymorphic deserialization based on sub types, there's an option to specify default implementation class in case none of the mapping matches

  • That seems to be missing while generating from openapi

  • Sample openapi spec

    ErrorInformation:
      discriminator:
        propertyName: error_code
        mapping:
          "XYZ": "#/components/schemas/XYZErrorInformation"
          "ABC": "#/components/schemas/ABCErrorInformation"
      properties:
        error_code:
          type: string
        message:
          type: string

    XYZErrorInformation:
      allOf:
        - $ref: "#/components/schemas/ErrorInformation"
      properties:
        xyz:
          type: string

    ABCErrorInformation:
      allOf:
        - $ref: "#/components/schemas/ErrorInformation"
      properties:
        abc:
          type: string
  • Corresponding java class generated
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)

@JsonIgnoreProperties(
  value = "error_code", // ignore manually set error_code, it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the error_code to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "error_code", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = ABCErrorInformation.class, name = "ABC"),
  @JsonSubTypes.Type(value = XYZErrorInformation.class, name = "XYZ")
})
public class ErrorInformation {

  private String errorCode;

  private String message;
}

Describe the solution you'd like

A property to specify default implementation in case none of the sub type matches in discriminator mentioned

    ErrorInformation:
      discriminator:
        propertyName: error_code
        mapping:
          "XYZ": "#/components/schemas/XYZErrorInformation"
          "ABC": "#/components/schemas/ABCErrorInformation"
       default_mapping: "#/components/schemas/ErrorInformation"
      properties:
        error_code:
          type: string
        message:
          type: string

This should generate java class with this annotation

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property = "error_code",
        visible = true,
        defaultImpl = ErrorResponse.class
)

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