2015-01-09 7 views
8

Scrivere il modello di swagger per un array JSON mi sembra piuttosto semplice, ad es. se ho avuto questa matrice:Modello di Swagger per un array con elementi denominati

[ 
    { 
    "name": "dave", 
    "number": 123 
    }, 
    { 
    "name": "mary", 
    "number": 456 
    } 
] 

avrei scritto il seguente modello spavalderia per esso:

"schema": { 
    "type": "array", 
    "items": { 
    "$ref": "Student" 
    } 
} 

"Student": { 
    "id": "Student", 
    "required": [ 
    "name", 
    "number" 
    ], 
    "properties": { 
    "name": { 
     "type": "string" 
    }, 
    "number": { 
     "type": "integer", 
     "format": "int32" 
    } 
    } 
} 

Tuttavia, ho il seguente:

{ 
    "123": { 
    "name": "dave" 
    }, 
    "456": { 
    "name": "mary" 
    } 
} 

Come faccio a scrivere il modello per questo?

Grazie in anticipo.

+0

Questo non è un array, è un oggetto con una mappa al suo interno. È questo che stai cercando di descrivere o c'è un errore nell'esempio? Inoltre, quale versione di Swagger usi? – Ron

+0

Hai ragione, non è davvero un array. Non c'è errore, è esattamente il json che voglio descrivere, ma non sono sicuro di come farlo. Sto usando lo swagger 2.0 (anche se ammetto che ci sono 1.2 avanzi in quello che ho scritto). – DeZot

+0

Se si vuole evitare di digitare a mano, si può provare questo convertitore da JSON a Swagger Definizioni: https://roger13.github.io/SwagDefGen/ – Roger

risposta

10

Per descrivere il modello di richiesta, è necessario utilizzare le proprietà additionalProperties. Intendiamoci, questo è disponibile in Swagger 2.0 e non era disponibile nelle versioni precedenti.

"definitions": { 
    "Student": { 
     "type": "object", 
     "required": [ "name" ], 
     "properties": { 
      "name": { 
       "type": "string" 
      } 
     } 
    }, 
    "Students": { 
     "type": "object", 
     "additionalProperties": { 
      "$ref": "#/definitions/Student" 
     } 
    } 
} 

Sopra si vede il modello Student, che attualmente contiene il "nome" di proprietà, anche se presumo si aggiungerà più ad esso. Dall'esempio sopra, è richiesta la proprietà "name".

Il secondo modello è Students che è un oggetto che contiene una mappa (additionalProperties). Ogni proprietà è del tipo Student (fatto facendo riferimento al modello, ma teoricamente avrebbe potuto essere definito in linea).

L'unica cosa che non si può fare è dichiarare che la chiave (o il nome della proprietà) è un numero intero o di un determinato tipo. Che avrebbe potuto essere supportato con lo patternedProperties che non è disponibile in Swagger 2.0. In altre parole, non esiste un modo tecnico per limitare il contenuto della chiave.

+1

Grazie per questo. Mi sembra però che lo swagger-ui non lo supporti ancora perché, quando l'ho provato, tutto quello che ho visto era un oggetto vuoto. L'anteprima di swagger-editor mi mostra anche un oggetto vuoto, anche se non riporta alcun errore. – DeZot

+0

Vero, credo che l'interfaccia utente attualmente non supporti 'additionalProperties'. Sentiti libero di aprire un problema a riguardo. – Ron

Problemi correlati