Skip to content

[java] Incorrect code being generated with oneOf #912

@mukulb90

Description

@mukulb90
Description

[JAVA] Incorrect code generation when subtypes are involved.

openapi-generator version

openapi-generator-cli-3.2.3

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "info": {
    "title": "Mukul",
    "version": "2.0"
  },
  "paths": {
    "/catalog/{id}": {
      "get": {
        "operationId": "getCatalogItem",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "default": {
            "description": "default response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CatalogEntity"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "CatalogEntity": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "entityType": {
            "type": "string"
          }
        },
        "discriminator": {
          "propertyName": "entityType",
          "mapping": {
            "folder": "#/components/schemas/Folder",
            "source": "#/components/schemas/Source"
          }
        },
        "oneOf": [
          {
            "$ref": "#/components/schemas/Folder"
          },
          {
            "$ref": "#/components/schemas/Source"
          }
        ]
      },
      "Folder": {
        "type": "object",
        "properties": {
          "path": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "tag": {
            "type": "string"
          },
          "children": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CatalogItem"
            }
          }
        },
        "allOf": [
          {
            "$ref": "#/components/schemas/CatalogEntity"
          }
        ]
      },
      "Source": {
        "type": "object",
        "properties": {
          "tag": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "children": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CatalogItem"
            }
          }
        },
        "allOf": [
          {
            "$ref": "#/components/schemas/CatalogEntity"
          }
        ]
      }
    }
  }
}
Command line used for generation

java -jar openapi-generator-cli-3.2.3-20180824.092221-7.jar generate -i openapi-apiresource.json -o ./gen -g java

Expectation: Generated classes Space and Source java should extend CatalogEntity and should have properties declared in the json.

Output: It generates classes Spaces and Source and they do extend CatalogEntity but all the props are missing.

For example:-

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2018-08-27T11:13:31.931-07:00[America/Los_Angeles]")
public class Space extends CatalogEntity {

  @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 Space {\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    ");
  }

}

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