설명
OpenAPI 3.0의 components.responses는 재사용 가능한 응답 정의를 담습니다. 존재하지 않는 응답을 $ref로 참조하면 해당 상태 코드의 응답 설명, 헤더나 본문 구조를 문서에서 해석하지 못할 수 있습니다.
잠재적 영향
- API 사용자가 오류 응답이나 정상 응답의 처리 방법을 잘못 이해할 수 있습니다.
- 참조 오류 때문에 문서 검증이나 코드 생성이 실패할 수 있습니다.
해결 방법
응답 $ref가 실제 Response 객체를 가리키는지 확인하세요. 로컬 참조를 components.responses의 정의와 일치시키고, 응답의 description과 본문 스키마도 검토하세요. 변경 후 사용하는 상태 코드와 응답 구조가 실제 동작과 맞는지 검증하세요.
예시
다음은 404 응답 정의를 참조하는 비교입니다. 첫 예제의 NotRight는 존재하지 않습니다.
변경 전
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API Overview",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"operationId": "listVersionsv2",
"summary": "List API versions",
"responses": {
"404": {
"$ref": "#/components/responses/NotRight"
}
}
}
}
},
"components": {
"schemas": {
"Error": {
"type": "object",
"properties": {
"code": {
"type": "string"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
},
"responses": {
"NotFound": {
"description": "Resource not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
변경 후
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API Overview",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"operationId": "listVersionsv2",
"summary": "List API versions",
"responses": {
"404": {
"$ref": "#/components/responses/NotFound"
}
}
}
}
},
"components": {
"schemas": {
"Error": {
"type": "object",
"properties": {
"code": {
"type": "string"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
},
"responses": {
"NotFound": {
"description": "Resource not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
변경 후에는 NotFound를 참조해 응답 설명과 Error 스키마를 연결합니다. Error의 required는 스키마 수준에서 code와 message를 필수 필드로 지정합니다.