Search code examples
openapiopenapi-generatoropenapi-generator-maven-plugin

Inheritance from empty abstract class using openapi 3.0


I'm using open api 3.0.3 I went through the documentation and another similar question Swagger Inheritance and Composition.

But I was not successful in getting it to work.

In Java code here is what I want:

public abstract BaseClass{}

public Employee extends BaseClass {...}

Note that BaseClass is empty. I tried various approaches like using discriminator, all of etc. but it is not clear how to get this to work. Example:

  BaseClass:
  discriminator: classType
    required:
      - classType
    properties:
      entityType:
        type: string
        enum:
          - Employee

  Employee:
    allOf:
    - $ref: "BaseEntity"
    properties:
    #...properties defined here...

I understand that abstract is not supported by open api but perhaps there is some other mechanism like extension.

Wanted to see if anyone knows how to do this with open-api?

PS: Also tried something like this, did not work

BaseClass:
  type: object
  abstract: true
  properties:
    dummy:
      type: string
      description: "Dummy property to allow an empty base class"
      readOnly: true

I tried x-abstract : true as well, didn't work. I'm open to other suggesttion like manually defining BaseClass and somehow having openAPI Employee specification extend that


Solution

  • Your discriminator in the example you provided is incorrect. Discriminator doesn't take a value directly. It is an object with properties. You need to specify the propertyName for the discriminator in order for it to register.

    Try this:

    components:
      schemas:
        BaseClass:
          type: object
          required:
            - entityType
          properties:
            entityType:
              type: string
              enum:
                - Employee
          discriminator:
            propertyName: entityType
    
        Employee:
          allOf:
            - $ref: "#/components/schemas/BaseClass"
          properties:
            anotherThing:
              type: string
    

    This is the minimum change to get your schema above to work. However, it will not create an empty BaseClass. This is because the BaseClass must have the discriminator within it. That is expected in this type of inheritance. It will also not be abstract. However, everything else you have requested will work.


    EDIT:

    It seems you really want it to be abstract and empty. You mentioned you tried to do this with x-abstract. That's not a thing. You can make it a thing, but the list of supported vendor extensions makes no mention of it. You can't expect it to understand a vendor extension that you made up without telling it what it means. That is done via the mustache templates. You will have to add the extension x-abstract to your mustache template where you want it to work. There are plenty of examples online to show how to add the extension to the template. Here's a great example of how to use extensions to enhance your templates from a very smart person.