Skip to content

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

@Arbuste

Description

@Arbuste

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions