Swagger-php: Example or suggestion please, How to write documentation with swagger script when have two definition (Posts, Pagination) ?

Created on 13 Dec 2017  路  2Comments  路  Source: zircote/swagger-php

Api response looks as:

{
  "posts": [
    {
      "id": 1,
      ....
    }
  ],
  "pagination": {
    "page": 1,
    ....
  }
}

Most helpful comment

You need te create an additional schema which those 2 properties, you can either define that schema inside the response, but when it is used more than once you could also create a separate schema (Definition in v2) and use a reference to that.

An example of how to write it in v3:

/**
 * @OA\Get(path="/api/posts",
 *   @OA\Response(
 *      response=200,
 *      description="List posts",
 *      @OA\JsonContent(
 *        @OA\Property(
 *          property="posts",
 *          type="array",
 *          @OA\Items(ref="#/components/schemas/Post")
 *        ),
 *        @OA\Property(
 *          property="pagination",
 *          ref="#/components/schemas/pagination"
 *        )
 *      )
 *   )
 * )
 */

/**
 * @OA\Schema(
 *   schema="Post",
 *   @OA\Property(
 *     property="id",
 *     type="integer"
 *   )
 * )
 */

 /**
 * @OA\Schema(
 *   schema="pagination",
 *   @OA\Property(
 *     property="page",
 *     type="integer",
 *     minimum=1
 *   )
 * )
 */

All 2 comments

You need te create an additional schema which those 2 properties, you can either define that schema inside the response, but when it is used more than once you could also create a separate schema (Definition in v2) and use a reference to that.

An example of how to write it in v3:

/**
 * @OA\Get(path="/api/posts",
 *   @OA\Response(
 *      response=200,
 *      description="List posts",
 *      @OA\JsonContent(
 *        @OA\Property(
 *          property="posts",
 *          type="array",
 *          @OA\Items(ref="#/components/schemas/Post")
 *        ),
 *        @OA\Property(
 *          property="pagination",
 *          ref="#/components/schemas/pagination"
 *        )
 *      )
 *   )
 * )
 */

/**
 * @OA\Schema(
 *   schema="Post",
 *   @OA\Property(
 *     property="id",
 *     type="integer"
 *   )
 * )
 */

 /**
 * @OA\Schema(
 *   schema="pagination",
 *   @OA\Property(
 *     property="page",
 *     type="integer",
 *     minimum=1
 *   )
 * )
 */

You need te create an additional schema which those 2 properties, you can either define that schema inside the response, but when it is used more than once you could also create a separate schema (Definition in v2) and use a reference to that.

An example of how to write it in v3:

/**
 * @OA\Get(path="/api/posts",
 *   @OA\Response(
 *      response=200,
 *      description="List posts",
 *      @OA\JsonContent(
 *        @OA\Property(
 *          property="posts",
 *          type="array",
 *          @OA\Items(ref="#/components/schemas/Post")
 *        ),
 *        @OA\Property(
 *          property="pagination",
 *          ref="#/components/schemas/pagination"
 *        )
 *      )
 *   )
 * )
 */

/**
 * @OA\Schema(
 *   schema="Post",
 *   @OA\Property(
 *     property="id",
 *     type="integer"
 *   )
 * )
 */

 /**
 * @OA\Schema(
 *   schema="pagination",
 *   @OA\Property(
 *     property="page",
 *     type="integer",
 *     minimum=1
 *   )
 * )
 */

this worked for me

Was this page helpful?
0 / 5 - 0 ratings