1

I have a simple swagger.json file, Product rest api with 2 methods: post (add new product) and put (update) and I want to define "name", "price" fields of Product definition as required for POST method but not for a PUT method.

How can i do that without code duplication?

There is my swagger.json file

{ 
  "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"
        }
      }
    }
  }
}
nekishdev
  • 11
  • 2

0 Answers0