OpenAPI 2.0 모델 속성 이름과 의미 점검

속성 이름은 각 모델의 의미에 맞추고 불필요한 API 계약 변경을 피하세요.

설명

OpenAPI 2.0에서는 서로 다른 객체 스키마에 같은 속성 이름을 사용할 수 있습니다. 예를 들어 여러 모델에 name이 있어도 유효합니다. 이름은 API 전체에서 고유하게 만들기보다 각 객체의 데이터 의미를 명확하게 표현해야 합니다.

잠재적 영향

  • 서로 다른 의미에 모호한 이름을 쓰면 API 사용자가 데이터를 잘못 이해할 수 있습니다.
  • 이미 사용 중인 속성을 불필요하게 바꾸면 기존 클라이언트와 호환되지 않을 수 있습니다.

해결 방법

각 객체 안에서 속성의 이름과 의미를 확인하세요. 다른 모델과 이름이 같다는 이유만으로 바꾸지 마세요. 같은 구조는 필요에 따라 $ref로 재사용하고, 실제 이름 변경이 필요하면 클라이언트 호환성과 마이그레이션을 계획하세요.

예시

다음 두 본문 스키마는 name, address, age를 각각 사용할 수 있습니다. 참조하는 Address 정의는 생략했습니다.

변경 전

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "basePath": "/api",
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "address": {
                  "$ref": "#/definitions/Address"
                },
                "age": {
                  "type": "integer",
                  "format": "int32"
                }
              }
            }
          }
        ]
      }
    }
  },
  "parameters": {
    "limitParam": {
      "name": "limit",
      "in": "body",
      "description": "max records to return",
      "required": true,
      "schema": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "address": {
            "$ref": "#/definitions/Address"
          },
          "age": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
}

변경 후

json
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "basePath": "/api",
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "name": "limit2",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string"
                },
                "address": {
                  "$ref": "#/definitions/Address"
                },
                "age": {
                  "type": "integer",
                  "format": "int32"
                }
              }
            }
          }
        ]
      }
    }
  },
  "parameters": {
    "limitParam": {
      "name": "limit",
      "in": "body",
      "description": "max records to return",
      "required": true,
      "schema": {
        "type": "object",
        "properties": {
          "name_2": {
            "type": "string"
          },
          "address_2": {
            "$ref": "#/definitions/Address"
          },
          "age_2": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
}

변경 후에는 재사용 매개변수의 속성에 _2를 붙였습니다. 이는 선택적인 API 계약 변경이며 전역 이름 중복을 없애기 위한 필수 조치가 아닙니다. 의미가 같은 속성에는 기존 이름을 유지해도 됩니다.

참조