Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 of 6 tasks
Arbuste opened this issue Aug 1, 2019 · 3 comments · Fixed by #3771
Closed
5 of 6 tasks

Comments

@Arbuste
Copy link

Arbuste commented Aug 1, 2019

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.

@sohrab-
Copy link

sohrab- commented Aug 2, 2019

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

@Arbuste
Copy link
Author

Arbuste commented Aug 2, 2019

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).

@andys8
Copy link
Contributor

andys8 commented Aug 6, 2019

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

#2845

mnahkies added a commit to mnahkies/openapi-generator that referenced this issue Aug 28, 2019
Whilst the spec states that the 'allOf' relationship does not imply a hierarchy:

> While composition offers model extensibility, it does not imply a hierarchy between the models.
> To support polymorphism, the OpenAPI Specification adds the discriminator field.

Unfortunately this does not make sense for many existing use cases, that were supported by older
versions of the generator. Therefore, I've restored the older behavior, specifically
in the case that only a single possible parent schema is present.

I think a more complete solution would generate interfaces for the composed schemas,
and mark the generated class as implementing these.

fixes issue 2845, and fixes issue OpenAPITools#3523
wing328 pushed a commit that referenced this issue Aug 28, 2019
…ts (#3771)

Whilst the spec states that the 'allOf' relationship does not imply a hierarchy:

> While composition offers model extensibility, it does not imply a hierarchy between the models.
> To support polymorphism, the OpenAPI Specification adds the discriminator field.

Unfortunately this does not make sense for many existing use cases, that were supported by older
versions of the generator. Therefore, I've restored the older behavior, specifically
in the case that only a single possible parent schema is present.

I think a more complete solution would generate interfaces for the composed schemas,
and mark the generated class as implementing these.

fixes issue 2845, and fixes issue #3523
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants