swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Java] Client Code Generation: Allof and Enum Inheritance

Open ThomasTosik opened this issue 4 years ago • 14 comments

Hi,

we have some issues generating a java client when allof/ref and enums are involved. Sadly we need the allof and we can not omit it since the yaml is given like that.

Tested with swagger cli 3.0.18 and 3.0.21 (on Java 11)

Yaml spec example:

openapi: 3.0.1
info:
  title: test
  version: '1.0'
paths:
  /api/v1/test:
    get:
      tags:
        - test
      summary: test
      description: 'test'
      operationId: test
      parameters:
        - name: level
          in: query
          description: test
          schema:
            allOf:
              - $ref: '#/components/schemas/TraceLevel'
            description: test
      responses:
        '204':
          description: Success
components:
  schemas:
    TraceLevel:
      enum:
        - Test
      type: string

The resulting java model of Level results in an extension of the TraceLevel enum and some overrides.

public class Level extends TraceLevel {

  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    return super.equals(o);
  }

  @Override
  public int hashCode() {
    return Objects.hash(super.hashCode());
  }


  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Level {\n");
    sb.append("    ").append(toIndentedString(super.toString())).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }

}

Erros when building:

[ERROR] Level.java:[24,8] enum types are not extensible
[ERROR] Level.java:[27,18] equals(java.lang.Object) in io.swagger.client.model.Level cannot override equals(java.lang.Object) in java.lang.Enum
[ERROR] overridden method is final
[ERROR] Level.java:[38,14] hashCode() in io.swagger.client.model.Level cannot override hashCode() in java.lang.Enum

Any ideas? Thanks for the help.

Regards

ThomasTosik avatar Sep 03 '20 09:09 ThomasTosik

I have the same issue with C# generated code. It ends up trying to inherit from an Enum which is impossible. I feel that the model should not be generated when having a AllOf ref. In my case I end up having a bunch of AllOf... classes all trying to inherit from that same Enum. Which is silly.

frjol avatar Oct 21 '20 18:10 frjol

Luckily we use NSwag for C# and do not have these issues there.

ThomasTosik avatar Oct 22 '20 07:10 ThomasTosik

We have the same problem with swagger-codegen. Classes inheriting from enum.

ioudas avatar Nov 03 '20 15:11 ioudas

Is there any progress on this item or a workaround?

harmvanderwal avatar Jan 27 '21 11:01 harmvanderwal

Is there any progress on this item or a workaround?

Yes there is a workaround. We just changed our spec from "lang": {"allOf":[{"$ref":"#/components/schemas/Language"}],"description":"language of incoming query"} to "lang": {"$ref": "#/components/schemas/Language"}

j3t avatar Feb 11 '21 09:02 j3t

Same issue with OpenJDK 8, swagger-codegen-maven-plugin 3.0.23 and this API spec: https://listing-creation.api.autoscout24.com/assets/openapi/spec.yml

Tingil79 avatar Mar 02 '21 15:03 Tingil79

Hi,

I'm hitting the same issue, but unable to use the workaround as tie reference is expanded on:

"allOf" : [ {
          "$ref" : "#/components/schemas/MetricEventAlertingScope"
        }, {
          "type" : "object",
          "properties" : {
            "nameFilter" : {
              "$ref" : "#/components/schemas/MetricEventTextFilterMetricEventTextFilterOperatorDto"
            }
          }
        } ]

stevenpost avatar Apr 20 '21 16:04 stevenpost

Any resolution for this

sourav-jha avatar Sep 03 '21 05:09 sourav-jha

The issue is still present in the latest client code generator 3.0.33.

dlasak avatar Mar 23 '22 09:03 dlasak

3.0.51 still has this problem.

onacit avatar Dec 27 '23 04:12 onacit

3.0.52 still has this problem.

onacit avatar Jan 31 '24 07:01 onacit

3.0.54 still has this problem.

onacit avatar Feb 22 '24 03:02 onacit

We are currently facing the exact same problem.

gianluca-moro avatar Apr 08 '24 11:04 gianluca-moro

Same problem in my php project

Teclor avatar Apr 25 '24 08:04 Teclor