mbknor / mbknor-jackson-jsonSchema

Generate JSON Schema with Polymorphism using Jackson annotations
MIT License
234 stars 79 forks source link

How to document type properties used for polymorphism? #171

Open JoergSiebahn opened 2 years ago

JoergSiebahn commented 2 years ago

Hi,

when I have a polymorphic class and add @JsonSchemaDescription at the visible type property, the description is ignored and not put in the Schema.

While generating the schema, I see this log (thanks for that, guess it saved some hours of research):

WARN com.kjetland.jackson.jsonSchema.JsonSchemaGenerator - Ignoring property 'type' in [simple type, class java.lang.String] since it has already been added, probably as type-property using polymorphism

Here is a simple example to reproduce:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    property = "type",
    include = JsonTypeInfo.As.EXISTING_PROPERTY,
    visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = SomeSuperclass.SomeSubType.class, name = "some"),
  @JsonSubTypes.Type(value = SomeSuperclass.SomeOtherSubType.class, name = "other")
})
public class SomeSuperclass {

  @JsonSchemaDescription("Defines the type.")
  private String type;

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public static class SomeSubType extends SomeSuperclass {}

  public static class SomeOtherSubType extends SomeSuperclass {}
}

I get JSON Schema for the two subtypes like this:

    SomeOtherSubType:
      type: "object"
      additionalProperties: true
      properties:
        type:
          type: "string"
          enum:
          - "other"
          default: "other"
      title: "other"
      required:
      - "type"
    SomeSubType:
      type: "object"
      additionalProperties: true
      properties:
        type:
          type: "string"
          enum:
          - "some"
          default: "some"
      title: "some"
      required:
      - "type"

So my description is missing. I als tried to merge the description in the property spec with different variants of @JsonSchemaInject and @JsonSchemaString at the property, the super class and the sub classes. None of my variants did the trick.

Is there any workaround to add a description to a type property that is used with @JsonTypeInfo?

Thanks in advance for any ideas that help.

My desired output would be like this:

    SomeOtherSubType:
      type: "object"
      additionalProperties: true
      properties:
        type:
          description: "Defines the type."
          type: "string"
          enum:
          - "other"
          default: "other"
      title: "other"
      required:
      - "type"
    SomeSubType:
      type: "object"
      additionalProperties: true
      properties:
        type:
          description: "Defines the type."
          type: "string"
          enum:
          - "some"
          default: "some"
      title: "some"
      required:
      - "type"