OpenAPITools / OpenAPITools/openapi-generator
[REQ] Support for abstract classes in inheritance hierarchies
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
My team manages a lot of analytical tools that involve data sets with complex inheritance relationships. As a result, most of our APIs involve schemas with inheritance, even if it's just describing data.
For example, right now we're working on a tool that allows analysts to upload and analyze data with dynamic schemas defined by the analyst. We define our schemas like this:
components:
schemas:
FieldType:
type: object
properties:
family:
type: string
nullable: false
required:
- family
discriminator:
propertyName: family
mapping:
boolean: '#/components/schemas/BooleanFieldType'
date: '#/components/schemas/DateFieldType'
enum: '#/components/schemas/EnumFieldType'
float: '#/components/schemas/FloatFieldType'
int: '#/components/schemas/IntFieldType'
slug: '#/components/schemas/SlugFieldType'
text: '#/components/schemas/TextFieldType'
BooleanFieldType:
allOf:
- $ref: '#/components/schemas/FieldType'
- type: object
DateFieldType:
allOf:
- $ref: '#/components/schemas/FieldType'
- type: object
EnumFieldType:
allOf:
- $ref: '#/components/schemas/FieldType'
- type: object
properties:
array:
type: boolean
nullable: false
values:
type: array
nullable: false
items:
type: string
required:
- array
- values
FloatFieldType:
allOf:
- $ref: '#/components/schemas/FieldType'
- type: object
IntFieldType:
allOf:
- $ref: '#/components/schemas/FieldType'
- type: object
SlugFieldType:
allOf:
- $ref: '#/components/schemas/FieldType'
- type: object
TextFieldType:
allOf:
- $ref: '#/components/schemas/FieldType'
- type: object
The generated code for this is already quite good. However, we'd like to be able to indicate that the FieldType object is an abstract base class.
Describe the solution you'd like
We'd like to be able to mark specific classes as abstract. For instance, we'd like to be able to indicate that the FieldType object is an abstract base class in the previous example. Certainly there are a lot of ways one could do this, but the simplest would be introducing a new specific extension attribute x-abstract for this purpose. That would make the FieldType declaration look like this:
FieldType:
type: object
x-abstract: true
properties:
family:
type: string
nullable: false
required:
- family
discriminator:
propertyName: family
mapping:
boolean: '#/components/schemas/BooleanFieldType'
date: '#/components/schemas/DateFieldType'
enum: '#/components/schemas/EnumFieldType'
float: '#/components/schemas/FloatFieldType'
int: '#/components/schemas/IntFieldType'
slug: '#/components/schemas/SlugFieldType'
text: '#/components/schemas/TextFieldType'
Describe alternatives you've considered
I'm aware that the JSONSschema team is working on some ways to indicate that classes are abstract from reviewing this issue.
I like that OpenAPI keeps up-to-date with the JSONSchema project a lot! And once that standard supports this use case, I think OpenAPI should support that change. However, it's been a year since that was written and the JSONSchema draft does not yet appear to be ready, so I see value in being expeditious, too. Being an extension, it would be simple to add and remove later when the time is right.
Certainly, this could also be handled with file post-processors. However, providing this feature declaratively in the file is preferable.
Additional context
If there's interest and this is an interesting feature, then I'd be happy to start work on a patch and make a PR. Otherwise, just thanks to the OpenAPI team for the great work and such a useful tool!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue proposes an x-abstract OpenAPI extension for schemas such as FieldType, which uses allOf and a discriminator. No source files, tests, or entry points are named, so start by locating how schema inheritance and discriminators are processed. Done means marked schemas are treated as abstract across generated output without affecting unmarked schemas.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100