user1563721
user1563721

Reputation: 1571

Java generate OpenAPI specification with discriminators annotations

I am trying to generate proper OpenAPI spec for the KeyData class in Java using swagger annotation and using discriminatorMapping.

There is a value property of KeyValue that depends on the format property of KeyFormat. The @Schema is defined like following (extract):

@Schema(
        description = "Format of the Key",
        requiredMode = Schema.RequiredMode.REQUIRED
)
private KeyFormat format;

@Schema(
        description = "Value of the Key",
        requiredMode = Schema.RequiredMode.REQUIRED,
        discriminatorProperty = "format",
        discriminatorMapping = {
                @DiscriminatorMapping(value = "Raw", schema = RawKeyValue.class),
                @DiscriminatorMapping(value = "SubjectPublicKeyInfo", schema = SpkiKeyValue.class),
                @DiscriminatorMapping(value = "PrivateKeyInfo", schema = PrkiKeyValue.class),
                @DiscriminatorMapping(value = "EncryptedPrivateKeyInfo", schema = EprkiKeyValue.class),
                @DiscriminatorMapping(value = "Custom", schema = CustomKeyValue.class)
        },
        oneOf = {
                RawKeyValue.class,
                SpkiKeyValue.class,
                PrkiKeyValue.class,
                EprkiKeyValue.class,
                CustomKeyValue.class
        }
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "format")
@JsonSubTypes({
        @JsonSubTypes.Type(value = RawKeyValue.class, name = "Raw"),
        @JsonSubTypes.Type(value = SpkiKeyValue.class, name = "SubjectPublicKeyInfo"),
        @JsonSubTypes.Type(value = PrkiKeyValue.class, name = "PrivateKeyInfo"),
        @JsonSubTypes.Type(value = EprkiKeyValue.class, name = "EncryptedPrivateKeyInfo"),
        @JsonSubTypes.Type(value = CustomKeyValue.class, name = "Custom")
})
private KeyValue value;

The expectation is that the OpenAPI spec will contain discriminator that will be used to identify which schema should be used for KeyValue based on the KeyFormat value. However, the generated yaml does not contain any information about it:

    KeyData:
      required:
      - algorithm
      - format
      - length
      - type
      - value
      type: object
      properties:
        type:
          $ref: '#/components/schemas/KeyType'
        algorithm:
          $ref: '#/components/schemas/KeyAlgorithm'
        format:
          $ref: '#/components/schemas/KeyFormat'
        value:
          $ref: '#/components/schemas/KeyValue'
        length:
          type: integer
          description: Bit length of the Key
          format: int32
        metadata:
          type: array
          description: "Metadata for the Key, specific data that can be technology\
            \ specific"
          items:
            $ref: '#/components/schemas/MetadataAttribute'
      description: Data of the Key

I am playing around with formatting of the schema but not able to generate proper OpenAPI spec with discriminator. Without that the generated clients have issues when unmarshalling the json because the client cannot match the object with proper schema.

What is the proper way to include discriminator for the KeyValue? I would like to manage it according the value in the format field.

Upvotes: 1

Views: 491

Answers (0)

Related Questions