스키마 속성의 readOnly와 writeOnly 충돌

같은 속성의 readOnly와 writeOnly를 모두 true로 설정하지 마세요.

설명

OpenAPI 3.0에서 readOnly: true는 응답에 사용할 속성을, writeOnly: true는 요청에 사용할 속성을 설명합니다. 같은 속성에 둘 다 true로 설정하면 사용 방향이 충돌하며 명세에 어긋납니다.

잠재적 영향

  • API 사용자가 속성을 요청에 보내야 하는지 응답에서 받아야 하는지 혼동할 수 있습니다.
  • 코드 생성기나 검증 도구가 정의를 거부하거나 요청과 응답 모델을 잘못 구성할 수 있습니다.

해결 방법

응답 전용 속성에는 readOnly만 true로, 요청 전용 속성에는 writeOnly만 true로 지정하세요. 양쪽에서 사용하는 속성은 둘 다 생략하거나 false로 두세요. 실제 서버의 입력 처리와 응답 직렬화가 이 계약을 따르는지도 확인하세요.

예시

객체 스키마의 정수 id 속성을 비교합니다. GeneralError를 사용하는 요청이나 응답 정의는 생략했습니다.

변경 전

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.c"
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "writeOnly": true,
            "readOnly": true
          },
          "code": {
            "type": "integer",
            "format": "int32"
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "name"
        ]
      }
    }
  }
}

변경 후

json
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.c"
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "readOnly": true
          },
          "code": {
            "type": "integer",
            "format": "int32"
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "name"
        ]
      }
    }
  }
}

변경 후 id는 readOnly: true만 유지해 응답에 사용할 속성임을 명시합니다. 스키마를 수정하는 것만으로 서버의 입력 처리나 접근 제어가 변경되지는 않습니다.

참조