2015-07-13 11 views
27

Ho una richiesta POST che utilizza il seguente corpo della richiesta JSON. Come posso descrivere questo corpo di richiesta usando OpenAPI (Swagger)?Come descrivere questo corpo di richiesta POST JSON in OpenAPI (Swagger)?

{ 
    "testapi": { 
     "testapiContext": { 
      "messageId": "kkkk8", 
      "messageDateTime": "2014-08-17T14:07:30+0530" 
     }, 
     "testapiBody": { 
      "cameraServiceRq": { 
       "osType": "android", 
       "deviceType": "samsung555" 
      } 
     } 
    } 
} 

Finora ho provato la seguente, ma mi sono bloccato a definire il corpo schema.

swagger: "2.0" 
info: 
    version: 1.0.0 
    title: get camera 
    license: 
    name: MIT 
host: localhost 
basePath: /test/service 
schemes: 
    - http 
consumes: 
    - application/json 
produces: 
    - application/json 
paths: 
    /getCameraParameters: 
    post: 
     summary: Create new parameters 
     operationId: createnew 
     consumes: 
     - application/json 
     - application/xml 
     produces: 
     - application/json 
     - application/xml 
     parameters: 
     - name: pet 
      in: body 
      description: The pet JSON you want to post 
      schema: # <--- What do I write here? 

      required: true 
     responses: 
     200: 
      description: "200 response" 
      examples: 
      application/json: 
      { 
       "status": "Success" 
      } 

Desidero definire il corpo di input in linea, come esempio per la documentazione.

risposta

25

ho fatto il lavoro con:

post: 
     consumes: 
     - application/json 
     produces: 
     - application/json 
     - text/xml 
     - text/html 
     parameters: 
     - name: body 
      in: body 
      required: true 
      schema: 
      # Body schema with atomic property examples 
      type: object 
      properties: 
       testapi: 
       type: object 
       properties: 
        messageId: 
        type: string 
        example: kkkk8 
        messageDateTime: 
        type: string 
        example: '2014-08-17T14:07:30+0530' 
       testapiBody: 
       type: object 
       properties: 
        cameraServiceRq: 
        type: object 
        properties: 
         osType: 
         type: string 
         example: android 
         deviceType: 
         type: string 
         example: samsung555 
      # Alternatively, we can use a schema-level example 
      example: 
       testapi: 
       testapiContext: 
        messageId: kkkk8 
        messageDateTime: '2014-08-17T14:07:30+0530' 
       testapiBody: 
        cameraServiceRq: 
        osType: android 
        deviceType: samsung555 
3

Il modo più leggibile per includere uno scalare multilinea in YAML è utilizzare lo block literal style. Ciò richiede di cambiare il vostro esempio JSON solo utilizzando indentazione (che verrà rimosso se si recupera il valore per la chiave):

. 
. 
produces: 
    - application/json 
example: | 
    { 
     "testapi": { 
      "testapiContext": { 
       "messageId": "kkkk8", 
       "messageDateTime": "2014-08-17T14:07:30+0530" 
    }, 
      "testapiBody": { 
       "cameraServiceRq": { 
        "osType": "android", 
        "deviceType": "samsung555" 
       } 
      } 
     } 
    } 
paths: 
    /getCameraParameters: 
. 
. 

(per chiarezza si può mettere un ritorno a capo o due prima che la chiave di scalare paths ., ottengono clipped by default sulle scalari stile blocco letterali

+0

Ora quando copio il jSON nel corpo della richiesta durante l'invio della richiesta che aggiunge un sacco di \ t \ n a il mio oggetto JSON, come posso inviare un JSON pulito alla schiena fine – Gaurav

Problemi correlati