2017-04-27 4 views
1

Swagger 2.0の助けを借りてAPIドキュメントを作成しています。私は1つのAPIを生成しました。応答は書籍の配列にあり、うまくいきます。Swagger 2.0のJSONオブジェクトのスキーマタイプとは

[{ 
    "id": 1, 
    "book_name": "The Complete Reference Java8", 
    "author": "Herbert Schidt", 
    "genre": "Technology" 
}, { 
    "id": 2, 
    "book_name": "C Programming", 
    "author": "Dennis Ritchie", 
    "genre": "Technology" 
}] 

闊歩

/fetchBooks: 
get: 
    description: | 
    Returns an array of book objects. 

    responses: 
    200: 
     description: Successful response 
     schema: 
     title: ArrayOfBooks 
     type: array 
     items: 
      type: object 
      properties: 
      id: 
       type: integer 
      book_name: 
       type: string 
      author: 
       type: string 
      genre: 
       type: string 

まあ、私は、オブジェクトが動作していないしようとしたとして、私はそれのために取るべきスキーマタイプJSONObjectに1つのAPIで1本のみの詳細を送信します。

{ 
    "id": 1, 
    "book_name": "The Complete Reference Java8", 
    "author": "Herbert Schidt", 
    "genre": "Technology" 
} 

闊歩

/fetchBook: 
get: 
    description: | 
    Returns a book object 

    parameters: 
    - name: id 
     in: query 
     description: Books Id's 
     reqrequired: true 
     type: integer 
     format: int 

    responses: 
    200: 
     description: Successful response 
     schema: 
     type: object <-- What type should I specify for JSONObject here 
     items: 
      type: object 
      properties: 
      id: 
       type: integer 
      book_name: 
       type: string 
      author: 
       type: string 
      genre: 
       type: string 

オブジェクトが動作していないように、闊歩はJSONフォーマットを示していません。

現状:

enter image description here

期待される状態:

enter image description here

答えて

2
/fetchBook: 
    get: 
     description: | 
     Returns a book object 

     parameters: 
     - name: id 
      in: query 
      description: Books Id's 
      required: true 
      type: integer 
      format: int 

     responses: 
     '200': 
      description: Successful response 
      schema: 
      type: object 
      properties: 
       id: 
       type: integer 
       book_name: 
       type: string 
       author: 
       type: string 
       genre: 
       type: string 

あなたが持っていた問題は、必要な

を提出し、休閑があるのタイプミスでした正しいsy単一のオブジェクト応答のntax

+0

「必須」に修正しました。現在の状態はhttps://i.stack.imgur.com/lM4ui.pngと同じで、予想状態はhttps://i.stack.imgur.com/997tK.png –

+0

にする必要があります。休止状態のリンクは、この回答では、[link](https://pastebin.com/raw/NFkxjDZz)、私はあなたのくぼみやコードを効果的にすることができる他の要因を調べることをお勧めします。この甘いyamlはあなたが望む応答を再現します – codeWisperer

+0

ありがとう!それは働いた –

2

ヒント:複数の操作で同じスキーマを再利用する場合は、 BookArrayOfBooksがある場合は、definitionsセクションにスキーマを定義し、別の場所に$refを定義できます。

paths: 
    /fetchBooks: 
    get: 
     ... 
     responses: 
     200: 
      description: Successful response 
      schema: 
      $ref: '#/definitions/ArrayOfBooks' # <-------- 
    /fetchBook: 
    get: 
     ... 
     responses: 
     200: 
      description: Successful response 
      schema: 
      $ref: '#/definitions/Book'   # <-------- 

definitions: 
    Book: 
    type: object 
    properties: 
     id: 
     type: integer 
     book_name: 
     type: string 
     author: 
     type: string 
     genre: 
     type: string 
    ArrayOfBooks: 
    type: array 
    items: 
     $ref: '#/definitions/Book' # <-------- 


これは新しい開発中のAPIはなく、既存のAPIである場合にも、GET /fetchBooksに "フェッチ"(フェッチ= GET)冗長です。 「フェッチ」を削除し、ちょうどGET /booksGET /book?id=...を使用することを検討してください。

+1

ヘレンの素晴らしいアプローチを提案してくれてありがとう。 +1 –

関連する問題