除非使用editor.swagger.io

时间:2017-07-18 13:43:17

标签: java jax-rs swagger swagger-codegen

我遇到了Swagger YAML和代码生成的问题。

我正在尝试从这个非常简单的代码段生成代码:

swagger: '2.0'
info:
  version: 1.0.0
  title: Number API
host: localhost
basePath: /v2
schemes:
- http
paths:
  '/numbers':
    get:
      summary: Get number
      description: Returns a number
      operationId: getNumber
      produces:
      - application/json
      responses:
        '200':
          description: successful operation
          schema:
            $ref: '#/definitions/Numbers'
definitions:
  Numbers:
    $ref: '#/definitions/Number'
  Number:
    type: number

我使用swagger-codegen-cli-2.2.3.jar执行以下命令:

java -jar swagger-codegen-cli-2.2.3.jar generate -i swagger.yaml -l jaxrs-spec -o ./

现在,预期输出是一个非常简单的API实现,其中/ numbers端点的响应是BigDecimal。如果我使用Swagger Editor,我会使用Swagger generator

但是,使用上面的命令,我得到一个API实现,其中响应尝试返回一个名为" Numbers"的模型。模型实现如下所示:

package io.swagger.model;

import javax.validation.constraints.*;


import io.swagger.annotations.*;
import java.util.Objects;


public class Numbers   {



  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Numbers numbers = (Numbers) o;
    return true;
  }

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

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Numbers {\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    ");
  }
}

这是不正确的,因为Numbers对象应该只是转换为BigDecimal,就像Swagger Generator正在做的那样..

任何人都可以向我解释为什么会发生这种情况,以及如何使generator-cli按预期运行?

修改 我查看了editor.swagger.io和codegen-cli生成的JSON。 editor.swagger.io中的JSON随生成的代码一起出现在ZIP存档中。它看起来像这样:

Swagger编辑器JAXRS-SPEC的JSON输出:

{
  "swagger" : "2.0",
  "info" : {
    "version" : "1.0.0",
    "title" : "Number API"
  },
  "host" : "localhost",
  "basePath" : "/v2",
  "schemes" : [ "http" ],
  "paths" : {
    "/numbers" : {
      "get" : {
        "summary" : "Get number",
        "description" : "Returns a number",
        "operationId" : "getNumber",
        "produces" : [ "application/json" ],
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "type" : "number"
            }
          }
        }
      }
    }
  },
  "definitions" : {
    "Numbers" : {
      "type" : "number"
    },
    "Number" : {
      "type" : "number"
    }
  }
}

而codegen-cli生成的JSON如下所示:

来自codegen-cli v2.2.3,v2.3.0,github master和所有其他版本的codegen-cli的JSON

{
  "swagger" : "2.0",
  "info" : {
    "version" : "1.0.0",
    "title" : "Number API"
  },
  "host" : "localhost",
  "basePath" : "/v2",
  "schemes" : [ "http" ],
  "paths" : {
    "/numbers" : {
      "get" : {
        "summary" : "Get number",
        "description" : "Returns a number",
        "operationId" : "getNumber",
        "produces" : [ "application/json" ],
        "parameters" : [ ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/Numbers"
            }
          }
        }
      }
    }
  },
  "definitions" : {
    "Numbers" : {
      "$ref" : "#/definitions/Number"
    },
    "Number" : {
      "type" : "number"
    }
  }
}

所以在Swagger编辑器版本中,refs被翻译成简单类型,而refs保留在codegen-cli版本中。

0 个答案:

没有答案