openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA] Generator does not generate inheritance when first element of allOf is not a ref

Open Arbuste opened this issue 7 years ago • 3 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Bounty to sponsor the fix (example)
Description

When declaring a schema as a composition of classes using allOf, having the first element of the allOf (which is meant to be the main inherited class extended in my Java subclass) not being a reference to a pre-existing class, it is simply ignored by the generator.

openapi-generator version

Swagger-editor and openapi-codegen 4.1.0

OpenAPI declaration file content or url
  schemas:
    Example1:
      type: object
      properties:
        Example1Property:
          type: string
    Example2:
      allOf:
        - type: object
          properties:
            Example2Property:
              type: string
        - $ref: '#/components/schemas/Example1'

The Swagger-editor generated Example2 class writes:

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2019-08-01T11:15:04.683Z[GMT]")
public class Example2 implements  Example1 {
  @SerializedName("Example1Property")
  private String example1Property = null;

  public Example2 example1Property(String example1Property) {
    this.example1Property = example1Property;
    return this;
  }

   /**
   * Get example1Property
   * @return example1Property
  **/
  @Schema(description = "")
  public String getExample1Property() {
    return example1Property;
  }

  public void setExample1Property(String example1Property) {
    this.example1Property = example1Property;
  }

// all the usual methods...

The openapi-codegen generated Example2 class writes:

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2019-08-02T09:54:03.694+02:00[Europe/Paris]")public class Example2 {

  @JsonProperty("Example1Property")

  private String example1Property = null;
  public Example2 example1Property(String example1Property) {
    this.example1Property = example1Property;
    return this;
  }

  

  /**
  * Get example1Property
  * @return example1Property
  **/
  @Schema(description = "")
  public String getExample1Property() {
    return example1Property;
  }
  public void setExample1Property(String example1Property) {
    this.example1Property = example1Property;
  }

// all the usual methods...

Expected output would have been (from openapi-codegen):

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2019-08-02T09:54:03.694+02:00[Europe/Paris]")public class Example2 {

  @JsonProperty("Example2Property")
  @JsonProperty("Example1Property")

  private String example2Property = null;
  private String example1Property = null;

// ...
Command line used for generation

"C:\Program Files\Java\jdk1.8.0_72\bin\java" -jar swagger-codegen-cli.jar generate -i myDefs.yml -l java --library resttemplate -DsupportJava6=true -DdateLibrary=legacy -o out2/myproject --api-package com.my.project.client.api --model-package com.my.project.client.model -DhideGenerationTimestamp=true -DdisableApiModelAnnotations=true

Suggest a fix

Having the same behaviour as when the non pre-existing class is set as more than the first occurrence in the allOf and is generated as an interface, that is copying the attributes of the non pre-existing class in the subclass.

Arbuste avatar Aug 01 '19 11:08 Arbuste

I think you have the wrong project. The code looks to be generated using swagger-codegen, not openapi-generator.

sohrab- avatar Aug 02 '19 06:08 sohrab-

I think you have the wrong project. The code looks to be generated using swagger-codegen, not openapi-generator.

You're right, I updated the post with a snippet of the openapi-codegen generated class (same problem though).

Arbuste avatar Aug 02 '19 08:08 Arbuste

It looks like inheritance is broken for several languages with 4.0.3. 4.0.0-beta3 is working for me.

https://github.com/OpenAPITools/openapi-generator/issues/2845

andys8 avatar Aug 06 '19 13:08 andys8