laravel resource swagger schema 설정

Updated on

use OpenApi\Attributes\Items;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

/** @property-read CompanyData $resource */
#[Schema(
    properties: [
        new Property(property: 'id', type: 'int', example: 1),
        new Property(property: 'name', type: 'string', example: '반짝보석'),
        new Property(property: 'number', type: 'string', example: '010-1111-2222'),
        new Property(property: 'company_licenses', properties: [
            new Property('name', type: 'string', example: '반짝이는 보석'),
            new Property('business_number', type: 'string', example: '111-22-33333'),
        ]),
        new Property(property: 'company_contacts', type: 'array', items: new Items(CompanyContactResource::class)),
    ]
)]
class CompanyResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->resource->id,
            'name' => $this->resource->name,
            'number' => $this->resource->number,
            'company_licenses' => new CompanyLicensesResource($this->resource->whenLoaded('company_licenses')),
            'company_contacts' => CompanyContactResource::collection($this->resource->company_contacts),
        ];
    }
}

laravel 에서 resource 작업할때 자동완성 및 swagger schema 설정하는 방법이다.