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

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

Open
CSALS opened this issue May 15, 2024 · 0 comments

Comments

@CSALS
Copy link

CSALS commented May 15, 2024

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
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant