1

我有一个简单的 swagger.json 文件,带有 2 种方法的 Product rest api:发布(添加新产品)和 put(更新),我想根据 POST 方法的要求定义产品定义的“名称”、“价格”字段,但不是对于 PUT 方法。

我怎么能在没有代码重复的情况下做到这一点?

有我的 swagger.json 文件

{ 
  "paths" : {
    "/products" : {
      "post" : {
        "summary" : "Add a new product",
        "operationId" : "addProduct",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "data",
          "description" : "Product object that needs to be added to the store",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/Product",
            "required": ["name", "price"] // <-------- not working
          }
        } ]
      },
    },
    "/products/{id}" : {
      "put" : {
        "summary" : "Update a product",
        "operationId" : "updateProduct",
        "consumes" : [ "application/json", "multipart/form-data" ],
        "produces" : [ "application/json" ],
        "parameters" : [
          {
            "in" : "path",
            "name" : "id",
            "description" : "Product id",
            "required" : true,
            "type": "integer",
            "format": "uint"
          },
          {
            "in" : "body",
            "name" : "data",
            "description" : "Product data for update",
            "required" : true,
            "schema" : {
              "$ref" : "#/definitions/Product"
            }
          }
        ]
      }
    }
  },
  "definitions" : {
    "Product": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name" : {
          "type" : "string"
        },
        "price": {
          "type": "number",
          "format": "float"
        }
      }
    }
  }
}
4

0 回答 0