2017-07-09 29 views
0

我在我的数据库扬鞭和JSON补丁

{ 
    partnerName: '24 Fitness', 
    supportedProducts: [ 
     'FitBit', 
     'Protein Powder' 
    ] 
}, 

以下对象结构,其中所述键值supportedProducts可以从客户端进行修改。

我正在构建一个PATCH API方法,使用swagger文档来支持上述功能。但我不确定修补程序对象的定义,因为文档没有提供构建PATCH的详细示例。

,我有当前定义在执行结束错误的,看起来像如下

"patch":{ 
    "description":"Update supported products for a partner", 
    "operationId":"Update supported products", 
    "parameters":[ 
     { 
     "name": "partnerName", 
     "in": "path", 
     "required": true, 
     "type": "string" 
     }, 
     { 
     "name": "supportedProducts", 
     "in": "body", 
     "required": true, 
     "schema":{ 
      "$ref":"#/definitions/PatchRequest" 
     } 
     } 
    ], 
    "responses":{ 
     "200":{ 
     "description": "product updated" 
     }, 
     "404":{ 
     "description": "Not Found" 
     } 
    } 

    "definitions": { 
    "PatchRequest":{ 
     "type": "object", 
     "required":[ 
     "partnerName", 
     "supportedProducts" 
     ], 
     "properties":{ 
     "partnerName":{"type": "string"}, 
     "supportedProducts":{ 
      "type": "array", 
      "items":{"type": "string"} 
     } 
     } 

    } 
    } 
+0

您的服务器期望什么请求的URL和主体? – Helen

+0

@Helen请求URL是/ data/{partnerName},body应该指定partnerName和supportedProducts作为值。但在我的情况下,当我尝试配置上述json时,失败了,我可以运行服务器并发出请求之前的方式 – user8222014

+0

这是您的整个JSON定义还是仅提取它? (因为这个代码本身不是一个有效的JSON。) – Helen

回答

0

对于这个简单的例子,我会用JSON Patch对象来描述的操作,使目标。 这里是一个JSON补丁Swagger API的example

paths: 
    /users/{GUID}: 
    patch: 
     summary: Update a user 
     parameters: 
     - name: GUID 
      in: path 
      required: true 
      type: string 
      format: GUID 
      description: The GUID of a specific user 
     - name: JsonPatch 
      in: body 
      required: true 
      schema: 
      $ref: "#/definitions/PatchRequest" 
     responses: 
     '200': 
      description: Successful response 
      schema: 
      $ref: "#/definitions/User" 
definitions: 
    PatchRequest: 
    type: array 
    items: 
     $ref: "#/definitions/PatchDocument" 
    PatchDocument: 
    description: A JSONPatch document as defined by RFC 6902 
    required: 
    - "op" 
    - "path" 
    properties: 
    op: 
     type: string 
     description: The operation to be performed 
     enum: 
     - "add" 
     - "remove" 
     - "replace" 
     - "move" 
     - "copy" 
     - "test" 
    path: 
     type: string 
     description: A JSON-Pointer 
    value: 
     type: object 
     description: The value to be used within the operations. 
    from: 
     type: string 
     description: A string containing a JSON Pointer value.