Skip to content

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

@JLLeitschuh

Description

@JLLeitschuh
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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions