OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.24k stars 6.43k forks source link

[BUG][JAVA][Spring HATEOAS] Generated collection has wrong type. LIst instead of CollectionModel #9062

Open deempl opened 3 years ago

deempl commented 3 years ago
Description

I try to use Generator together with spring HATEOAS. It is generating me interface method with looks like this:

ResponseEntity<List<Item>> getItem();

Item class is generated correctly to support HATEOAS:

public class Item extends RepresentationModel<Item>

The problem is that method returns a List<Item> instead of : org.springframework.hateoas.CollectionModel<T> so finally Spring HATEOAS is not creating correct HAL response body.

Can I somehow force generator to use CollectionModel instead of List?

Documentation: 2.4.2. Collection resource representation mode

openapi-generator version

5.0.0

OpenAPI declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
albertotn commented 2 years ago

I was facing same problem as you, and with this simple yaml example:

openapi: 3.0.1
info:
  title: test
  description: 'test'
  version: 1.0.0
tags:
- name: hello
  description: hello service
paths:
  /hello:
    get:
      tags:
      - hello
      summary: Hello world service
      description: hello worlds simple service
      operationId: helloWorld
      responses:
        200:
          description: successful operation
          content:
            application/hal+json:
              schema:
                $ref: "#/components/schemas/HelloResponse"
        400:
          description: Invalid status value
          content: {}
components:
  schemas:
    HelloResponse:
      type: object
      properties:
        msg:
          type: string

and with java spring generator and in particular on gradle hateos enabled like:

..
openApiGenerate {
    generatorName = "spring"
    inputSpec = "$rootDir/src/main/resources/openApi/hello.yaml".toString()
    outputDir = "$buildDir/openApi/generated".toString()
    apiPackage = "testPackage"
    invokerPackage = "testPackage"
    modelPackage = "testPackage"
    configOptions = [
        dateLibrary: "java8",
        hateoas : "true"
    ]
}
..

generated the following model:

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.openapitools.jackson.nullable.JsonNullable;
import javax.validation.Valid;
import javax.validation.constraints.*;
import org.springframework.hateoas.RepresentationModel;

/**
 * HelloResponse
 */
public class HelloResponse extends RepresentationModel<HelloResponse>   {
  @JsonProperty("msg")
  private String msg;

  public HelloResponse msg(String msg) {
    this.msg = msg;
    return this;
  }

  /**
   * Get msg
   * @return msg
  */
  @ApiModelProperty(value = "")

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    HelloResponse helloResponse = (HelloResponse) o;
    return Objects.equals(this.msg, helloResponse.msg);
  }

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

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class HelloResponse {\n");

    sb.append("    msg: ").append(toIndentedString(msg)).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(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

this is fine also for you ?

I am using org.openapitools:openapi-generator-gradle-plugin:5.3.0

jfbourner commented 2 months ago

I'm also facing the same issue where the API generated does not return the correct type.