설명
OpenAPI 3.0에서 components.requestBodies의 요청 본문 정의를 $ref로 재사용할 수 있습니다. 참조 대상이 없으면 요청의 미디어 타입과 데이터 구조를 문서 도구가 해석하지 못할 수 있습니다.
잠재적 영향
- API 사용자가 요청 본문의 형식이나 필드를 잘못 이해할 수 있습니다.
- 클라이언트 생성이나 문서 검증이 참조 오류로 실패할 수 있습니다.
해결 방법
요청 본문의 $ref를 실제 Request Body 객체와 연결하세요. 로컬 참조는 components.requestBodies의 이름과 일치시키고, 본문의 content와 스키마 참조도 확인하세요. 수정 후 실제 요청 형식과 문서가 일치하는지 검증하세요.
예시
POST 요청에서 재사용하는 JSON 본문을 비교합니다. 첫 예제의 MyWrongObjectBody는 정의되어 있지 않습니다.
변경 전
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API Overview",
"version": "1.0.0"
},
"paths": {
"/": {
"post": {
"operationId": "listVersionsv2",
"summary": "List API versions",
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyObject"
}
}
}
}
},
"requestBody": {
"$ref": "#/components/requestBodies/MyWrongObjectBody"
}
}
}
},
"components": {
"schemas": {
"MyObject": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
},
"requestBodies": {
"MyObjectBody": {
"description": "A JSON object containing my object information",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyObject"
}
}
}
}
}
}
}
변경 후
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API Overview",
"version": "1.0.0"
},
"paths": {
"/": {
"post": {
"operationId": "listVersionsv2",
"summary": "List API versions",
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyObject"
}
}
}
}
},
"requestBody": {
"$ref": "#/components/requestBodies/MyObjectBody"
}
}
}
},
"components": {
"schemas": {
"MyObject": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
},
"requestBodies": {
"MyObjectBody": {
"description": "A JSON object containing my object information",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyObject"
}
}
}
}
}
}
}
변경 후에는 MyObjectBody를 참조하고, 이 본문은 MyObject 스키마로 JSON 구조를 설명합니다. 응답의 스키마는 별도로 응답 content 안에 배치했습니다.