openapi-generator: [BUG] No more inheritance for components generated by version 5.0.0

Description

No schema inheritance.

openapi-generator version

5.0.0

OpenAPI declaration file content or url
schemas:
  UserDTO:
      type: object
      properties:
        id:
          type: string
          minLength: 1
          maxLength: 20

  QueryUserResponseDTO:
      type: object
      allOf:
        - $ref: '#/components/schemas/UserDTO'
Generation Details

The above YAML produces:

public class QueryUserResponseDTO   {
   private @Valid String id;
}

Instead as for the 4.3.1:

public class QueryUserResponseDTO extends UserDTO   {
}
Suggest a fix

Does exist a configuration parameter to enforce the DTO inheritance generation? Or it is a bug?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 16
  • Comments: 21 (10 by maintainers)

Most upvoted comments

The problem is that the base class can’t always know all the classes that will extend it. In my case, #9756 it isn’t even necessary as the base class isn’t used for Serialization/Deserialization. But I need the base class for methods that operate on the fields it provides. In my environment we have a “BasePage” object for paged requests. All the services that need paging extend that. BasePage can’t possibly know all the services that will support paging as it is in a common library that the services import. This change has made it impossible to support this.

Just to confirm. I updated from 4.3.1 -> 5.0.0 and I’m seeing the same issue (with dotnet-core generator) and AllOf inheritance. For us, this makes vers 5.0.0 unusable.

If any help, here is a git diff of 4.3.1 generated class and 5.0.0 generated class which shows the issue:

  • superclass gone form class signature
  • base construtor not called
  • superclass properties added to subclass
namespace FOOBAR.Model
 {
@@ -29,7 +29,7 @@ namespace FOOBAR.Model
     /// ProductBankLoan
     /// </summary>
     [DataContract(Name = "ProductBankLoan")]
-    public partial class ProductBankLoan : Product, IEquatable<ProductBankLoan>
+    public partial class ProductBankLoan : IEquatable<ProductBankLoan>, IValidatableObject
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="ProductBankLoan" /> class.
@@ -38,9 +38,12 @@ namespace FOOBAR.Model
         /// <param name="productID">productID.</param>
         /// <param name="debtors">debtors.</param>
         /// <param name="collaterals">collaterals.</param>
-        public ProductBankLoan(double balance = default(double), int productID = default(int), List<Debtor> debtors = default(List<Debtor>), List<Collateral> collaterals = default(List<Collateral>)) : base(productID, debtors, collaterals)
+        public ProductBankLoan(double balance = default(double), int productID = default(int), List<Debtor> debtors = default(List<Debtor>), List<Collateral> collaterals = default(List<Collateral>))
         {
             this.Balance = balance;
+            this.ProductID = productID;
+            this.Debtors = debtors;
+            this.Collaterals = collaterals;
         }

Hi all, I’ve filed https://github.com/OpenAPITools/openapi-generator/pull/14172 to allow using $ref as parent in allOf with a new option called “openapi-normalizer”.

Please give it a try as follows:

git clone --depth 1 -b allof-parent-type https://github.com/OpenAPITools/openapi-generator/
cd openapi-generator
mvn clean package -DskipTests -Dmaven.javadoc.skip=true
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i /Users/williamcheng/Code/openapi-generator7/modules/openapi-generator/src/test/resources/3_0/allOf_extension_parent.yaml -o /tmp/java-okhttp/ --additional-properties hideGenerationTimestamp="true" --openapi-normalizer REF_AS_PARENT_IN_ALLOF=true

We hit the same issue with model composition as described here: https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

@antonio-petricca I’m getting this from the log when running the generator:

[main] INFO  o.o.codegen.utils.ModelUtils - [deprecated] inheritance without use of 'discriminator.propertyName' has been deprecated in the 5.x release. Composed schema name: null. Title: null
[main] INFO  o.o.codegen.utils.ModelUtils - [deprecated] inheritance without use of 'discriminator.propertyName' has been deprecated in the 5.x release. Composed schema name: null. Title: null

I havn’t had time to dive into it fully yet (we’ve reverted to 4.3.1 for now) but from what I can gather it seems that my swagger.json doesnt have the discriminator field on the supertype. However I’m using asp.net swashbuckle to generate the swagger.json/.yaml which is setup to OAS3. So what I havn’t figured out yet is if the problem really is the yaml not being generated correctly by swashbuckle (i.e. missing the discriminator field). I cant seem to get it to generate the discriminator field. (yet - working on it when I have time)

You need to actually add a discriminator to your base class and then add the annotation [SwaggerDiscriminator(“discriminatorPropertyName”)] as well. That has worked for me.

I am going to revert to 4.3.1. 😦