farit-slv/yii2-oas3

Yii2 Open Api Swagger 3 integration

Installs: 70

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 11

Type:yii2-extension

v0.4 2023-07-12 07:42 UTC

This package is auto-updated.

Last update: 2024-11-12 10:34:20 UTC


README

Requirements

  • PHP 7.4
  • Yii2 Framework

Installation

The preferred way to install this wrapper is through composer.

php composer.phar require farit-slv/yii2-oas3 "*"

or

composer require farit-slv/yii2-oas3 "*"

or add to the require section of composer.json

"farit-slv/yii2-oas3" : "*"

Integration

Add action to web controller (for example SiteController.php):

public function actions()
{
    return [
        'api-redoc' => [
            'class' => 'farit-slv\swagger\ViewAction',
            'apiJsonUrl' => \yii\helpers\Url::to(['/site/api-json'], true),
            'useReDoc' => true,
        ],
        'api-docs' => [
            'class' => 'farit-slv\swagger\ViewAction',
            'apiJsonUrl' => \yii\helpers\Url::to(['/site/api-json'], true),
        ],
        'api-json' => [
            'class' => 'farit-slv\swagger\JsonAction',
            'dirs' => [
                Yii::getAlias('@api/modules/api/controllers'),
                Yii::getAlias('@api/modules/api/models'),
                Yii::getAlias('@api/models'),
            ],
        ],
    ];
}

Open Api Swagger 3 example annotation

Api server description

/**
 * @OA\Info(
 *   version="1.0",
 *   title="Application API",
 *   description="Server - Mobile app API",
 *   @OA\Contact(
 *     name="John Smith",
 *     email="john@example.com",
 *   ),
 * ),
 * @OA\Server(
 *   url="https://example.com/api",
 *   description="main server",
 * )
 * @OA\Server(
 *   url="https://dev.example.com/api",
 *   description="dev server",
 * )
 */
 
class DefaultController extends Controller
{
...

Controller annotation

/**
 * @OA\Get(path="/",
 *   summary="Handshake",
 *   tags={"handshake"},
 *   @OA\Parameter(
 *     name="access-token",
 *     in="header",
 *     required=false,
 *     @OA\Schema(
 *       type="string"
 *     )
 *   ),
 *   @OA\Response(
 *     response=200,
 *     description="Returns Hello object",
 *     @OA\MediaType(
 *         mediaType="application/json",
 *         @OA\Schema(ref="#/components/schemas/Hello"),
 *     ),
 *   ),
 * )
 */
public function actionIndex()
{
... 

Model annotation

/**
 *@OA\Schema(
 *  schema="Hello",
 *  @OA\Property(
 *     property="message",
 *     type="string",
 *     description="Text message"
 *  ),
 *  @OA\Property(
 *     property="time",
 *     type="integer",
 *     description="Server current Unix time"
 *  ),
 *  @OA\Property(
 *     property="date",
 *     type="string",
 *     format="date-time",
 *     description="Server current date time"
 *  )
 *)
 */
class Hello extends Model
{
...

Donate

LICENSE

This curl wrapper is released under the MIT license.