如何用招摇描述JSON数组?

时间:2019-01-30 13:59:49

标签: swagger lumen

假设我有一个像这样的json:

{
    "cars": [
        { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
        { "name":"BMW", "models":[ "320", "X3", "X5" ] },
        { "name":"Fiat", "models":[ "500", "Panda" ] }
    ]
}

我该如何用招摇(最好是摇晃的流明)来形容?

1 个答案:

答案 0 :(得分:0)

要用夸张的方式描述“汽车”对象,首先在Json Schema中对其建模是一个方便的技巧:

{
   "type":"array",
   "items":{
      "type":"object",
      "required":[
         "name",
         "models"
      ],
      "properties":{
         "name":{
            "type":"string"
         },
         "models":{
            "type":"array",
            "items":{
               "type":"string"
            }
         }
      }
   }
}
  

此json代表请求正文,并通过post提交

然后在定义HTTP操作时可以引用该模型:

{
   "swagger":"2.0",
   "info":{
      "description":"My Cars definition\n",
      "version":"1.0.0",
      "title":"Cars"
   },
   "paths":{
      "/postMyCars":{
         "post":{ // <-- HTTP post
            "summary":"Post my cars in!",
            "consumes":[
               "application/json"
            ],
            "parameters":[
               {
                  "name":"cars",
                  "in":"body", // <-- in request body
                  "description":"Cars to add to the system",
                  "required":true,
                  "schema":{ // <-- define your cars payload here
                     "type":"array",
                     "items":{
                        "type":"object",
                        "required":[
                           "name",
                           "items"
                        ],
                        "properties":{
                           "name":{
                              "type":"string"
                           },
                           "models":{
                              "type":"array",
                              "items":{
                                 "type":"string"
                              }
                           }
                        }
                     }
                  }
               }
            ],
            "responses":{
               "200":{
                  "description":"Cars OK"
               }
            }
         }
      }
   },
   "schemes":[
      "https",
      "http"
   ]
}