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

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

설명

Swagger 2.0에서 서로 다른 객체 스키마는 같은 필드 이름을 사용할 수 있습니다. 각 객체에서 의미가 분명하면 이름을 유지하세요.

잠재적 영향

외부에 공개한 필드 이름을 바꾸면 기존 클라이언트가 데이터를 읽거나 전송하지 못할 수 있습니다.

해결 방법

이름이 모호하면 설명을 보완하고 공통 구조는 $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를 붙인 곳은 재사용 파라미터 limitParam뿐입니다. 예시의 GET 작업은 별도의 본문 스키마를 사용합니다.

참조