티스토리 뷰
이슈
`l5-swagger` 사용시 모델에 정의된 Schema에 Resource에 맞는 Property를 추가하는데 이슈가 있었다.
https://github.com/DarkaOnLine/L5-Swagger?tab=readme-ov-file
GitHub - DarkaOnLine/L5-Swagger: OpenApi or Swagger integration to Laravel
OpenApi or Swagger integration to Laravel. Contribute to DarkaOnLine/L5-Swagger development by creating an account on GitHub.
github.com
Github에도 자세한 메뉴얼이 없다보니 사용하는데 에로사항이 조금 있었는데, 우연히 검색하는 과정에서 발견하게 되어 작성해 본다.
구글에서 검색시 laravel 사용자는 swagger 사용시 l5-swagger를 많이 사용하는 듯한데 도움이 되지 않을까?
해결 방안
// AwesomeModel.php
<?php
#[OA\Schema(
type : 'object',
schema: "AwesomeModel",
properties: [
new OA\Property(property: 'id', type: 'integer', description: "id"),
new OA\Property(property: 'name', type: 'string', description: "name"),
new OA\Property(property: 'status', type: 'string', description: "상태"),
],
)]
class AwesomeModel {}
// AwesomeController.php
<?php
#[OA\Get(
...
)]
#[OA\Response(
response:200,
ref:"#/components/responses/AwesomeResource",
)]
#[OA\Response(response:400, ref:"#/components/responses/BadRequest")]
class AwesomeController {}
// AwesomeResource.php
<?php
#[OA\Response(
description:"Success",
response: "AwesomeResource",
content: new OA\JsonContent(
properties: [
new OA\Property(
property: 'data',
type: "array",
items: new OA\Items(
allOf:[
new OA\Schema(ref:"#/components/schemas/AwesomeModel"),
new OA\Schema(properties: [
new OA\Property(property:'created_at', type: 'string', format:"date", description: "생성일"),
]),
],
),
),
],
)
)]
class AwesomeResource extends JsonResource
{
public function toArray(Request $request)
{
$response = parent::toArray($request);
return [
...$response,
'created_at' => $this->anotherModel->createdAt,
];
}
}
요점은 `allOf`를 통해 여러개 스키마를 묶어 줄 수 있다.
allOf:[
new OA\Schema(),
new OA\Schema(),
],
참고
L5 Swagger - how to add examples for request body or response body
i'm trying to define some request body example in a file and link this file to the actual request, i saw we can use Swagger $ref to do that Reusing Examples but i can't find the correct L5 Swagger ...
stackoverflow.com
'공부합시다 > php' 카테고리의 다른 글
Laravel Event Observer 매개변수 전달하기 (0) | 2024.09.30 |
---|---|
Laravel에서 kafka 사용하기 laravel-kafka (0) | 2024.09.24 |
Laravel Carbon으로 addMonth 오류 (0) | 2024.04.05 |
Laravel Exception Test (0) | 2023.11.15 |
Laravel 다중 DB 사용시 테스트 트랜잭션 사용하기. (0) | 2023.11.13 |
- Total
- Today
- Yesterday
- laravel-kafka
- 정의
- POP3
- Laravel
- observer 매개변수 전달하기
- bitwarden-cli
- 라라벨
- 메일
- l5-swagger-response
- MySQL
- 정규식
- eloquent-observer
- graphql-php
- redis
- django
- addMonth
- php
- Python
- graphql
- vim
- password-manager
- 테스트_다중트랜잭션
- 자바스크립트
- php-laravel
- session+token authorize
- laravel-test
- exception-test
- l5-swagger
- aaa패턴
- addMonthWithoutOverflow
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |