Skip to content

[Java] Client Code Generation: Allof and Enum Inheritance #10446

Open

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

Activity

  1. frjol commented on Oct 21, 2020

    @frjol

    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.

  2. ThomasTosik commented on Oct 22, 2020

    @ThomasTosik
    Author

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

  3. ioudas commented on Nov 3, 2020

    @ioudas

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

  4. harmvanderwal commented on Jan 27, 2021

    @harmvanderwal

    Is there any progress on this item or a workaround?

  5. j3t commented on Feb 11, 2021

    @j3t

    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"}

  6. Tingil79 commented on Mar 2, 2021

    @Tingil79
  7. stevenpost commented on Apr 20, 2021

    @stevenpost

    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"
                }
              }
            } ]
    
  8. sourav-jha commented on Sep 3, 2021

    @sourav-jha

    Any resolution for this

  9. dlasak commented on Mar 23, 2022

    @dlasak

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

  10. onacit commented on Dec 27, 2023

    @onacit

    3.0.51 still has this problem.

  11. onacit commented on Jan 31, 2024

    @onacit

    3.0.52 still has this problem.

  12. onacit commented on Feb 22, 2024

    @onacit

    3.0.54 still has this problem.

  13. gianluca-moro commented on Apr 8, 2024

    @gianluca-moro

    We are currently facing the exact same problem.

  14. Teclor commented on Apr 25, 2024

    @Teclor

    Same problem in my php project

  15. JuergenSimon commented on Jul 21, 2025

    @JuergenSimon

    Issue persists on 3.0.71.

  16. JuergenSimon commented on Jul 21, 2025

    @JuergenSimon

    While the workaround of j3t does function, it is on limited use when you have a schema from an external source that you have no influence over in front of you. Of course I can go and manually update the document to remove all 'allOf:' occurences where a reference is used, but that means I can not fully automate the process. Please fix this.

  17. Teclor commented on Jul 21, 2025

    @Teclor

    While the workaround of @j3t does function, it is on limited use when you have a schema from an external source that you have no influence over in front of you. Of course I can go and manually update the document to remove all 'allOf:' occurences where a reference is used, but that means I can not fully automate the process. Please fix this.

    I made sh script, to remove such allOf blocks

    #!/bin/bash
    
    INPUT="../openapi.yaml"
    TMPFILE=$(mktemp)
    process_yaml() {
        local input="$1"
        local output="$2"
    
        local -a new_lines
        local line i=0 count changes=0
    
        mapfile -t lines < "$input"
    
        count=${#lines[@]}
    
        while (( i < count )); do
            line="${lines[$i]}"
    
            if [[ "$line" =~ ^([[:space:]]+)allOf:$ ]]; then
                indent="${BASH_REMATCH[1]}"
                echo "Found allOf at line $((i+1))"
    
                ((i++))
    
                if (( i >= count )) || ! [[ "${lines[$i]}" =~ ^${indent}[[:space:]]+- ]]; then
                    new_lines+=("$line")
                    continue
                fi
    
                ((i++))
                if (( i >= count )) || ! [[ "${lines[$i]}" == *\$ref:* ]]; then
                    new_lines+=("$line" "${lines[$i-1]}" "${lines[$i]}")
                    ((i++))
                    continue
                fi
    
                ((i++))
    
                if (( i >= count )) || ! [[ "${lines[$i]}" =~ ^${indent}[[:space:]]+- ]]; then
                    echo "Second element does not start with '-', skipping."
                    new_lines+=("$line" "${lines[$i-2]}" "${lines[$i-1]}" "${lines[$i]}")
                    continue
                fi
    
                ((i++))
                if (( i >= count )) || ! [[ "${lines[$i]}" == *properties:* ]]; then
                    echo "Second element does not contain properties, skipping."
                    new_lines+=("$line" "${lines[$i-3]}" "${lines[$i-2]}" "${lines[$i-1]}" "${lines[$i]}")
                    ((i++))
                    continue
                fi
    
                ((i++))
                prop_indent=$(echo "${lines[$i-1]}" | grep -o '^[[:space:]]*')
                new_lines+=("${indent}properties:")
    
                while (( i < count )) && [[ "${lines[$i]}" == ${prop_indent}* ]]; do
                    new_lines+=("${indent}${lines[$i]#${prop_indent}}")
                    ((i++))
                done
    
                ((changes++))
            else
                new_lines+=("$line")
                ((i++))
            fi
        done
    
        printf "%s\n" "${new_lines[@]}" > "$output"
        echo "Processing complete. $changes changes made."
    }
    
    process_yaml "$INPUT" "$TMPFILE"
    
    mv "$TMPFILE" "$INPUT"
    chmod 644 "$INPUT"
    
    echo "File updated: $INPUT"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions