[Java] "allOf" no longer supports inheritance AND composition

See original GitHub issue

Description

I just attempted to migrate from swagger-codegen to openapi-generator. It was mostly successful, except the behavior of allOf has changed. Previously the generated Java class would inherit from the first $ref and compose from the rest. With the new generator the Java class still extends the first $ref, as expected. But all the other $refs are ignored. (Their fields are no longer composed directly into the Java class.)

openapi-generator version

I am using the new Gradle plugin: openapi-generator-gradle-plugin:3.3.2 (much appreciated).

This previously worked with swagger-codegen-cli:3.0.2. In case you’re wondering, since there is no official Gradle plugin I was using this org.hidetake.swagger.generator, which wraps the CLI as a Gradle plugin.

OpenAPI declaration file

See comments next to each $ref:

    Foo:
      allOf:
      - $ref: "#/components/schemas/FooBase"  # generated Java extended this class (still works)
      - $ref: "#/components/schemas/FooComposed1"  # all fields inlined (no longer works)
      - $ref: "#/components/schemas/FooComposed2"  # all fields inlined (no longer works)
      - properties:  # other props also inlined (still works)
          fooCount:
            type: integer

Note: FooBase has a discriminator. The composed models do not:

    FooBase:
      discriminator:
        propertyName: fooType
      required:
      - fooType
      properties:
        fooType:
          type: string
Command line used for generation
build.gradle excerpt
openApiGenerate {
    generatorName = 'java'
    configFile = 'open-api-cfg.json'
}
config file
{
  "modelPackage"     : "com.foo",
  "serializableModel": "true",
  "dateLibrary"      : "java8",
  "java8"            : "true"
}
Suggest a fix/enhancement

The Java generator should support single inheritance, plus an arbitrary number of composed models, as it did before. This allows a great deal of flexibility. For example, I have models representing “views” returned to clients consisting of a database record that extends a base model, plus fields composed from other models. The client need not know which fields came directly from the db record, and which are calculated on-the-fly and returned from other models.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
wing328commented, Dec 10, 2018

Next release is 4.0.0 (tentatively scheduled on Jan 30th)

1reaction
wakedeercommented, Mar 13, 2020

Confirmed fix in 4.0.0-beta2. Thank you!

One anomaly: The following console warning is still emitted, even though the additional inline schemas beyond the first are “composed” (not ignored), as expected:

More than one inline schema specified in allOf:. Only the first one is recognized. All others are ignored.

Is there a way to avoid this warning? I use version 4.2.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSon schema and Inheritance - java - Stack Overflow
in short, no. schemas inside allOf , etc. know nothing of one another and cannot be "extended" in the object-oriented sense. spacetelescope.
Read more >
Support for oneOf, anyOf and allOf - Liferay Help Center
With allOf you can use the power of composition to create an entity that combines several others. It's the most potent way of...
Read more >
Favoring Composition Over Inheritance In Java With Examples
The fact that Java does not support multiple inheritances is one reason for favoring composition over inheritance in Java.
Read more >
Inheritance and Polymorphism - Swagger
OAS 3 This guide is for OpenAPI 3.0. Inheritance and Polymorphism. Model Composition. In your API, you may have model schemas that share...
Read more >
Language Guide (proto3) | Protocol Buffers - Google Developers
It will not be serialized to the wire. repeated : this field type can be repeated zero or more times in a well-formed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found