swagger-api / swagger-api/swagger-codegen

[Java] Client Code Generation: Allof and Enum Inheritance

Open
#10,446 17 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied OpenAPI YAML through the Java client generator and inspect the generated Level.java; compare how the allOf/$ref reference to enum TraceLevel is handled. Done means the generator produces compilable Java for this input without attempting enum inheritance or overriding final enum methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.