|
1 | 1 | # Collections |
2 | 2 |
|
| 3 | +Collections permit the declaration of multiple OpenAPI document 'collection' configurations. |
| 4 | + |
| 5 | +The openapi.php config file contains a single collection configuration by default, titled 'default'. |
| 6 | + |
| 7 | +Additional collection configurations may be added to the collections array, the key of the entry represents the name of the collection. |
| 8 | + |
| 9 | +Where Schemas should belong to specific collections, the 'Collection' annotation can be added to the class definition, with a name value matching the collection name e.g. |
| 10 | + |
| 11 | +```php |
| 12 | +namespace App\OpenApi\V1\Schemas; |
| 13 | + |
| 14 | +use Vyuldashev\LaravelOpenApi\Factories\SchemaFactory; |
| 15 | +use Vyuldashev\LaravelOpenApi\Contracts\Reusable; |
| 16 | +use Vyuldashev\LaravelOpenApi\Annotations as OpenApi; |
| 17 | + |
| 18 | +/** |
| 19 | + * @OpenApi\Collection(name = "v1") |
| 20 | + **/ |
| 21 | +class QuoteOfferSchema extends SchemaFactory implements Reusable |
| 22 | +{ |
| 23 | + ... |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Controller methods can also be assigned to a collection using the 'Collection' annotation. |
| 28 | + |
| 29 | +```php |
| 30 | +namespace App\Api\V1\Controllers; |
| 31 | + |
| 32 | +use Vyuldashev\LaravelOpenApi\Annotations as OpenApi; |
| 33 | + |
| 34 | +/** |
| 35 | + * @OpenApi\Collection(name = "v1") |
| 36 | + **/ |
| 37 | +class DemoController extends Controller |
| 38 | +{ |
| 39 | + /** |
| 40 | + * @param Request $request |
| 41 | + * |
| 42 | + * @return JsonResponse |
| 43 | + * |
| 44 | + * @OpenApi\Collection(name="v1") |
| 45 | + * @OpenApi\Operation(tags="demo") |
| 46 | + * @OpenApi\Response(factory="App\OpenApi\V1\Responses\DemoResponse", statusCode=200) |
| 47 | + */ |
| 48 | + public function create(Request $request): JsonResponse |
| 49 | + { |
| 50 | + ... |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Web |
| 56 | + |
| 57 | +To permit web UI routing to resolve specific collection documentation, it is necessary to override this package's provided OpenApiController and provide the generator's generate method with the collection name. |
| 58 | + |
| 59 | +One way of doing this is by using named route parameters, setting the opeanapi.php config collection's routes like so: |
| 60 | + |
| 61 | +```php |
| 62 | + 'route' => [ |
| 63 | + 'uri' => '/openapi/{collection}', |
| 64 | + 'middleware' => [...], |
| 65 | + ], |
| 66 | +``` |
| 67 | + |
| 68 | +Custom controller: |
| 69 | + |
| 70 | +```php |
| 71 | +<?php |
| 72 | + |
| 73 | +namespace App\OpenApi; |
| 74 | + |
| 75 | +use GoldSpecDigital\ObjectOrientedOAS\OpenApi; |
| 76 | +use Vyuldashev\LaravelOpenApi\Generator; |
| 77 | + |
| 78 | +class OpenApiController |
| 79 | +{ |
| 80 | + public function show(Generator $generator, string $collection): OpenApi |
| 81 | + { |
| 82 | + return $generator->generate($collection); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +You will then need to bind this controller in the register method of a service provider like so: |
| 88 | + |
| 89 | +```php |
| 90 | +<?php |
| 91 | + |
| 92 | +namespace App\Providers; |
| 93 | + |
| 94 | +use App\OpenApi\OpenApiController as CustomOpenApiController; |
| 95 | +use Illuminate\Support\ServiceProvider; |
| 96 | +use Vyuldashev\LaravelOpenApi\Http\OpenApiController; |
| 97 | + |
| 98 | +class AppServiceProvider extends ServiceProvider |
| 99 | +{ |
| 100 | + /** |
| 101 | + * Register any application services. |
| 102 | + * |
| 103 | + * @return void |
| 104 | + */ |
| 105 | + public function register() |
| 106 | + { |
| 107 | + $this->app->bind(OpenApiController::class, function ($app) { |
| 108 | + return $app->make(CustomOpenApiController::class); |
| 109 | + }); |
| 110 | + } |
| 111 | +``` |
| 112 | + |
| 113 | +## CLI |
| 114 | + |
| 115 | +The openapi:generate command takes an optional collection parameter, which is 'default' by default: |
| 116 | + |
| 117 | +The below example will generate the OpenAPI spec for a collection named 'v1', if it exists in the openapi.php config file's collections array: |
| 118 | + |
| 119 | +``` |
| 120 | +php artisan openapi:generate v1 |
| 121 | +``` |
| 122 | + |
0 commit comments