fabrikt icon indicating copy to clipboard operation
fabrikt copied to clipboard

External model reference doesn't generate JsonSubtypes or actual subtypes

Open rossLodge-R opened this issue 2 years ago • 1 comments

So given two files:

api.yaml:

openapi: 3.0.0
paths: {}
info:
  title: ""
  version: ""
components:
  schemas:
    Wrapper:
      type: object
      properties:
        polymorph:
          $ref: 'external-models.yaml#/components/schemas/PolymorphicEnumDiscriminator'

and external-models.yaml:

openapi: 3.0.0
paths: {}
info:
  title: ""
  version: ""
components:
  schemas:
    PolymorphicEnumDiscriminator:
      type: object
      discriminator:
        propertyName: some_enum
        mapping:
          obj_one_only: '#/components/schemas/ConcreteImplOne'
          obj_two_first: '#/components/schemas/ConcreteImplTwo'
          obj_two_second: '#/components/schemas/ConcreteImplTwo'
          obj_three: '#/components/schemas/ConcreteImplThree'
      properties:
        some_enum:
          $ref: '#/components/schemas/EnumDiscriminator'

    ConcreteImplOne:
      allOf:
        - $ref: '#/components/schemas/PolymorphicEnumDiscriminator'
        - type: object
          properties:
            some_prop:
              type: string

    ConcreteImplTwo:
      allOf:
        - $ref: '#/components/schemas/PolymorphicEnumDiscriminator'
        - type: object
          properties:
            some_prop:
              type: string
              
    ConcreteImplThree:
      allOf:
        - $ref: '#/components/schemas/PolymorphicEnumDiscriminator'

    EnumDiscriminator:
      type: string
      enum:
        - obj_one_only
        - obj_two_first
        - obj_two_second
        - obj_three

Generated source file PolymorphicEnumDiscriminator.kt is:

package com.example.models

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME,
  include = JsonTypeInfo.As.EXISTING_PROPERTY,
  property = "some_enum",
  visible = true
)
@JsonSubTypes()
sealed class PolymorphicEnumDiscriminator() {
  abstract val someEnum: EnumDiscriminator
}

I would expect it to work just as it does if the schemas are defined in the api.yaml file.

rossLodge-R avatar Sep 14 '22 22:09 rossLodge-R

This one could be difficult to fix. I have wasted an enormous amount of time trying to get external file handling robust and I always come up short. At this point I consider it best effort.

I will see what I can do, but it will be a time-boxed effort.

One workaround for trivial cases, is to use the fragment approach to merge multiple schemas together:

--api-fragment

cjbooms avatar Sep 15 '22 12:09 cjbooms