2013-10-25 11 views
31

Ho un servizi REST di documentare, alcuni di loro accetta semplice array come:Come descrivere un modello in Swagger per un array con oggetti semplici?

[ 
    { "name":"a" }, 
    { "name":"b" }, 
    { "name":"c" } 
] 

Come posso descrivere questo nella sezione relativa ai modelli Swagger? Posso solo creare 'il nome di matrice' come

model { 
properties: { "arr": { "type":"array", ...... 

ma descrive i dati in questo modo:

"arr": [ 
    { "name":"a" }, 
    { "name":"b" }, 
    { "name":"c" } 
] 
+2

Se si vuole evitare di battitura a mano, si potrebbe provare questo JSON al convertitore Swagger Definizioni: https://roger13.github.io/SwagDefGen/ – Roger

risposta

-2

Se la mia comprensione è corretta, credo che quanto segue può ciò che si desidera.

Come lei ha ricordato,

alcuni di loro accetta semplice array

allora sarebbe passato attraverso un parametro.

"parameters" : [{ 
    "name" : "param_name", 
    "type" : "array", 
    "items" : { 
    "$ref" : "M" 
    } 
    ... 
}] 
... 

Per la sezione del modello:

"models" : { 
    "M": { 
    "id" : "M", 
    "properties": { 
     "name" : { 
     "type" : "string" 
     } 
    } 
    } 
+13

Stavo per chiedere come descrivere: [{"nome": "a"}, {"nome": "b"}, {"nome": "c"}] – razor

7

Probabilmente simile a questo:

... 
    "parameters" : [{ 
     "name" : "whatEverThatArrayCalled", 
     "type" : "array", 
     "items" : { 
     "$ref" : "whatEverThatArrayCalled" 
     } 
     ... 
    }], 
    "models" : { 
    { 
    "id" : "whatEverThatArrayCalled", 
    "properties": 
     { 
     "whatEverThatArrayCalled" : 
      { 
     "type" : "array", 
     "items":{"name":"a", 
        "name":"b", 
        "name":"c" 
        }, 

      } 
     } 
    } 
}   

...

2

ho provato la follwoing nel editor.swagger.io, soddisfa la richiesta di questa domanda e funziona. Il corpo della richiesta POST si aspetta un array. La matrice è composta da elementi di "stackoverflow". Ogni oggetto è un oggetto, che ha la proprietà del nome.

paths: 
    /test: 
    post: 
     summary: test 123 
     description: test 123 
     parameters: 
     - name: param1 
      in: body 
      required: true 
      description: test param1 
      schema: 
      type: array 
      items: 
       $ref: '#/definitions/stackoverflow' 
     responses: 
     200: 
      description: OK 
definitions: 
    stackoverflow: 
    type: object 
    properties: 
     name: 
     type: string 
     description: name of the object 
+0

Questa è la risposta corretta . La chiave è 'in: body'. Secondo la specifica Swagger: "Poiché può esserci un solo carico utile, può esserci solo un parametro del corpo: il nome del parametro body non ha alcun effetto sul parametro stesso e viene utilizzato solo a scopo di documentazione." – jrc

7

Tony YUEN era vicino, ma senza sigaro. Questa è la definizione corretta utilizzando YAML in OpenAPI/Swagger:

/test: 
post: 
    summary: test 123 
    description: test 123 
    parameters: 
    - name: param1 
     in: body 
     required: true 
     description: test param1 
     schema: 
      $ref: '#/definitions/stackoverflow' 
    responses: 
    200: 
     description: OK 

Questo produce:

stackoverflow2[ 
    { 
    name: string 
    } 
] 

esempio di Tony produce:

[ 
    stackoverflow { 
       name: string 
    } 
] 

completa Swagger/OpenAPI come YAML (copia & incolla)

swagger: '2.0' 

################################################################################ 
#        API Information         # 
################################################################################ 
info: 
    version: "Two-point-Oh!" 
    title: Simple objects in array test 
    description: | 
    Simple objects in array test 

################################################################################ 
#         Parameters         # 
################################################################################ 

paths: 
    /test: 
    post: 
     summary: Array with named objects 
     description: Array with named objects 
     parameters: 
     - name: param1 
      in: body 
      required: true 
      description: test param1 
      schema: 
      type: array 
      items: 
       $ref: '#/definitions/stackoverflow' 
     responses: 
     200: 
      description: OK 
    /test2: 
    post: 
     summary: Array with simpel (nameless) objects 
     description: Array with simpel (nameless) objects 
     parameters: 
     - name: param1 
      in: body 
      required: true 
      description: test param1 
      schema: 
       $ref: '#/definitions/stackoverflow2' 
     responses: 
     200: 
      description: OK 
definitions: 
    stackoverflow: 
    type: object 
    properties: 
     name: 
     type: string 
     description: name of the object 
    stackoverflow2: 
    type: array 
    items: 
     type: object 
     properties: 
     name: 
      type: string 
      description: name of the object 

Ecco un JSON-versione di Swagger/OpenAPI

{ 
    "swagger" : "2.0", 
    "info" : { 
    "description" : "Simple objects in array test\n", 
    "version" : "Two-point-Oh!", 
    "title" : "Simple objects in array test" 
    }, 
    "paths" : { 
    "/test" : { 
     "post" : { 
     "summary" : "Array with named objects", 
     "description" : "Array with named objects", 
     "parameters" : [ { 
      "in" : "body", 
      "name" : "param1", 
      "description" : "test param1", 
      "required" : true, 
      "schema" : { 
      "type" : "array", 
      "items" : { 
       "$ref" : "#/definitions/stackoverflow" 
      } 
      } 
     } ], 
     "responses" : { 
      "200" : { 
      "description" : "OK" 
      } 
     } 
     } 
    }, 
    "/test2" : { 
     "post" : { 
     "summary" : "Array with simpel (nameless) objects", 
     "description" : "Array with simpel (nameless) objects", 
     "parameters" : [ { 
      "in" : "body", 
      "name" : "param1", 
      "description" : "test param1", 
      "required" : true, 
      "schema" : { 
      "$ref" : "#/definitions/stackoverflow2" 
      } 
     } ], 
     "responses" : { 
      "200" : { 
      "description" : "OK" 
      } 
     } 
     } 
    } 
    }, 
    "definitions" : { 
    "stackoverflow" : { 
     "type" : "object", 
     "properties" : { 
     "name" : { 
      "type" : "string", 
      "description" : "name of the object" 
     } 
     } 
    }, 
    "stackoverflow2" : { 
     "type" : "array", 
     "items" : { 
     "$ref" : "#/definitions/stackoverflow2_inner" 
     } 
    }, 
    "stackoverflow2_inner" : { 
     "properties" : { 
     "name" : { 
      "type" : "string", 
      "description" : "name of the object" 
     } 
     } 
    } 
    } 
} 
3

incolla questo per http://editor.swagger.io/#/ e clicca su "provare questa operazione"

{ 
    "swagger": "2.0", 
    "info": { 
    "version": "1.0.0", 
    "title": "Privacy-Service API" 
    }, 
    "paths": { 
    "/allNames": { 
     "post": { 
     "consumes": [ 
      "application/json" 
     ], 
     "produces": [ 
      "application/json" 
     ], 
     "parameters": [ 
      { 
      "name": "request", 
      "in": "body", 
      "schema": { 
       "$ref": "#/definitions/ArrayOfNames" 
      } 
      } 
     ], 
     "responses": { 
      "200": { 
      "description": "List of names", 
      "schema": { 
       "type": "array", 
       "items": { 
       "type": "string" 
       } 
      } 
      } 
     } 
     } 
    } 
    }, 
    "definitions": { 
    "ArrayOfNames": { 
     "type": "array", 
     "items": { 
     "minItems": 1, 
     "type": "object", 
     "required": [ 
      "name" 
     ], 
     "properties": { 
      "name": { 
      "type": "string" 
      } 
     } 
     } 
    } 
    } 
} 
+0

è possibile senza 'application/json'? – Gobliins

1
parameters: 
    - name: "items" 
    in: "formData" 
    description: "description" 
    required: "true" 
    type: "array" 
    items: 
     type: "object" 
     additionalProperties: 
     properties: 
      name: 
      type: "string" 

Secondo i loro documenti https://swagger.io/docs/specification/data-models/dictionaries/, questo dovrebbe tradursi in una matrice con oggetti che hanno una proprietà chiamata name e datatype è una stringa.
Vi si può accedere sul corpo richieste, qualcosa come request.body.items

Aggiornamento:

sembra che sia sufficiente per fare (senza le additionalproperties):

items: 
    type: object 
    properties: 
    name: 
     type: string 

Ora che hai gli articoli in cui ciascuno ha una chiave denominata nome e un valore corrispondente.

Se è questo, quello che il TO stava chiedendo ....

+2

Grazie per questo snippet di codice, che potrebbe fornire un aiuto limitato e immediato. Una [spiegazione corretta migliorerebbe notevolmente il suo valore a lungo termine] (// meta.stackexchange.com/q/114762/350567) mostrando * perché * questa è una buona soluzione al problema e lo renderebbe più utile per il futuro lettori con altre domande simili. Per favore [modifica] la tua risposta per aggiungere qualche spiegazione, incluse le ipotesi che hai fatto. – iBug

Problemi correlati