Swagger 生成重复模型 类

Swagger generating duplicated model classes

使用 springjaxrs-specthis minimal example 生成服务器代码会创建 2 个模型 classes:Pet.javaInlineResposne200.java。除了 class 名称外,它们完全相同。

控制器响应 /pets returns List<InlineResponse200> 而不是 List<Pet> 和 class Pet 实际上从未在任何地方使用,即使 yaml 定义使用 $ref: "#/definitions/Pet"。为什么会这样?

---
  swagger: "2.0"
  info: 
    version: "1.0.0"
    title: "Swagger Petstore"
    description: "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification"
    termsOfService: "http://swagger.io/terms/"
    contact: 
      name: "Swagger API Team"
    license: 
      name: "MIT"
  host: "petstore.swagger.io"
  basePath: "/api"
  schemes: 
    - "http"
  consumes: 
    - "application/json"
  produces: 
    - "application/json"
  paths: 
    /pets: 
      get: 
        description: "Returns all pets from the system that the user has access to"
        produces: 
          - "application/json"
        responses: 
          "200":
            description: "A list of pets."
            schema: 
              type: "array"
              items: 
                $ref: "#/definitions/Pet"
  definitions: 
    Pet: 
      type: "object"
      required: 
        - "id"
        - "name"
      properties: 
        id: 
          type: "integer"
          format: "int64"
        name: 
          type: "string"
        tag: 
          type: "string"

我尝试使用 springjaxrs-specswagger-codegen v2.2.2 复制它,但我无法获得 InlineResponse200.java.

但是,在进行了更多挖掘之后,我发现这个问题最近 reported 作为一个错误。它尚未修复,但 InlineResponse200.java 不应该存在。

此文件似乎是在使用 Swagger Editor v3 时错误生成的。我猜你就是这样生成文件的?

同时 Swagger Editor v2 does not have this problem, my suggestion for the time being for you would be to install and use the latest stable version of swagger-codegenSwagger.yaml 文件生成代码。

希望对您有所帮助。