swagger-api / swagger-api/swagger-codegen

[JAVA] Use interfaces in model when not using discriminator for inheritance?

Open
#5,275 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Feature: Composition / Inheritance General: Suggestion
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

If you choose not to use inheritance in your model and instead simply use composition using the allOf properties, does it make sense to generate interfaces for these models so that the server implementation can have code reuse for specific types?

Use case for not relying upon inheritance:
Inheritance makes the API more confusing for consumers in untyped languages. In our use case, our consumers primarily use untyped languages but our server is written in strongly typed java.

Swagger-codegen version

A version just a few commits after the 2.2.2 release.

Swagger declaration file content or url

For example:

definitions:

  ModelOne:
    type: object
    properties:
      modelOneProp:
        type: string

  ModelTwo:
    type: object
    properties:
      modelTwoProp:
        type: string

  ModelThree:
    type: object
    properties:
      modelThreeProp:
        type: string

  SimpleComposition:
    allOf:
      - $ref: '#/definitions/ModelOne'
      - $ref: '#/definitions/ModelTwo'
      - type: object
        properties:
          simpleCompositionProp:
            type: string

  CompositionOfSimpleComposition:
      allOf:
        - $ref: '#/definitions/SimpleComposition'
        - $ref: '#/definitions/ModelThree'
        - type: object
          properties:
            compositionOfSimpleCompositionProp:
              type: string
Suggest a Fix

In the above example additional interfaces would be generated (names are up for debate):
IModelOne, IModelTwo, IModelThree ISimpleComposition, ICompositionOfSimpleComposition.

The class ModelOne would implement IModelOne as would SimpleComposition and ISimpleComposition.

Ect...

What are the maintainers thoughts on this? Would this be a feature worth implementing in the mainline branch?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Java generator's handling of allOf composition and compare generated models for the YAML example. Confirm the interface names and implementation rules with maintainers before coding; done means the agreed interfaces are generated and implemented consistently for the composed models.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.