N Sharma
N Sharma

Reputation: 34507

What is the schema type for JSON object in swagger 2.0

I am writing a API document with the help of Swagger 2.0. I have generated one API where response is in array of books which is working fine.

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

Swagger

/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

Well, I want to send only one book details in one API in JSONObject what schema type should I take for it as I tried object is not working.

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

Swagger

/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

As object is not working, swagger is not showing JSON format.

Current State :

enter image description here

Expected State :

enter image description here

Upvotes: 1

Views: 1865

Answers (2)

Helen
Helen

Reputation: 98021

Tip: If you want to reuse the same schema in multiple operations - e.g. have a single Book and an ArrayOfBooks, you can define the schema in the definitions section and $ref it elsewhere.

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'  # <--------


Also, if this is a new API under development and not an existing API, "fetch" in GET /fetchBooks is redundant (GET = fetch). Consider dropping "fetch" and using just GET /books and GET /book?id=....

Upvotes: 2

codeWisperer
codeWisperer

Reputation: 86

  /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

the problem you had was a typo in the required filed

and the fallowing is the correct syntax for single Object responses

Upvotes: 2

Related Questions