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

user2441441
  • 1,237
  • 4
  • 24
  • 45

1 Answers1

2

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.

tbatch
  • 1,398
  • 10
  • 21
  • That's so great, thank you so much. Do we need the entityType do perform the inheritance? Can we have a dummy property just so the BaseClass is created? – user2441441 Apr 25 '23 at 23:49
  • yes. In fact, I do this in a lot of mine using a discriminator called with `propertyName: discriminate`. However, this could hurt Jackson's deserialization. It won't be able to find the base type. You could set up your classes to use Deduction instead, but that would take a lot more templating know-how. – tbatch Apr 26 '23 at 14:03
  • I want to save this using Spring data JPA, and I get an error saying entityType is not a column. I don't want a BaseEntity table. So is it possible to have only one column in BaseEntity and have Employee extend it. Employee database table has all Employee + BaseEntity columns. I'm also de-serializing with Jackson so I don't want Jackson deserialization to fail either – user2441441 May 09 '23 at 19:35
  • I'd recommend asking this as a new question, as it is beyond the scope of your original question. – tbatch May 09 '23 at 20:05
  • Posted a new question here - https://stackoverflow.com/questions/76220447/jpa-and-jackson-with-inheritance-how-to-get-this-to-work – user2441441 May 10 '23 at 15:52
  • ' I do this in a lot of mine using a discriminator called with propertyName: discriminate' can you plese elaborate? I need Open API to generate the extends relationship without using the discriminate – user2441441 May 25 '23 at 14:21
  • In contrast to what the answer suggests, it is not necessary to specify the `entityType` in the `properties`. The combination of `discriminator: entityType` and `required: ["entityType"]` is sufficient in terms of OpenAPI. But there is still a bug in the OpenAPI generator, that prevents generation of classes without `properties`: https://github.com/OpenAPITools/openapi-generator/issues/7638 – MyKey_ Jun 21 '23 at 17:09