OpenAPITools / OpenAPITools/openapi-generator

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

Open
#18,680 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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
)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Java generator code that emits @JsonTypeInfo and @JsonSubTypes from OpenAPI discriminator mappings. Check existing Java generator tests for polymorphic schemas, then define coverage for an unmapped discriminator value and verify that the generated annotation includes the requested default implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.