Skip to content

Latest commit

 

History

History
157 lines (124 loc) · 4 KB

File metadata and controls

157 lines (124 loc) · 4 KB

🇺🇸 English | 🇷🇺 Русский | 🇨🇳 中文

remove-parameter

从 OpenAPI 规范中删除端点的参数

配置

参数 描述 示例 类型 默认值
endpointDescriptor [必填] 要从中删除参数的端点描述 "GET /pets" string \ { path: string; method: string } -
parameterDescriptor [必填] 要删除的参数描述。在 in 参数中,可以指定:"query""path""header""cookie" {"name": "petId", "in": "path"} { name: string; in: "query" \ "path" \ "header" \ "cookie" } -

配置示例:

module.exports = {
    pipeline: [
        // ... 其他规则
        {
            rule: "remove-parameter",
            config: {
                endpointDescriptor: "GET /pets/{petId}", // 指定要从中删除参数的端点
                parameterDescriptor: {
                    name: "version", // 指定要删除的参数名称
                    in: "query" // 指定参数位置(查询参数)
                }
            },
        }
        // ... 其他规则
    ]
}

如果需要修改多个规范,您可以在整体配置管道中多次使用此规则。

动机

1. 需要从端点中删除未使用的参数,以停止使用它并在以后删除

实际示例:

openapi.yaml 文件中,端点文档如下所示:

paths:
  /pets/{petId}:
    get:
      summary: Get pet by ID
      parameters:
        - name: petId
          in: path
          required: true
          schema:
            type: string
        - name: version
          in: query
          required: false
          schema:
            type: string

需要删除未使用的参数 version

在配置文件 openapi-modifier-config.js 中,添加 remove-parameter 规则:

module.exports = {
    pipeline: [
        {
            rule: "remove-parameter",
            config: {
                endpointDescriptor: "GET /pets/{petId}",
                parameterDescriptor: {
                    name: "version",
                    in: "query"
                }
            },
        }
    ]
}

应用规则后openapi.yaml 文件如下所示:

paths:
  /pets/{petId}:
    get:
      summary: Get pet by ID
      parameters:
        - name: petId
          in: path
          required: true
          schema:
            type: string

2. 需要删除干扰 TypeScript 类型生成的公共参数

实际示例:

openapi.yaml 文件中,有一个带参数的组件:

components:
  parameters:
    ApiKeyHeader:
      name: X-API-Key
      in: header
      required: true
      schema:
        type: string

需要删除 ApiKeyHeader 参数组件。

在配置文件 openapi-modifier-config.js 中,添加 remove-parameter 规则:

module.exports = {
    pipeline: [
        {
            rule: "remove-parameter",
            config: {
                parameterDescriptor: {
                    name: "X-API-Key",
                    in: "header"
                }
            },
        }
    ]
}

应用规则后openapi.yaml 文件如下所示:

components:
  parameters: {}

重要说明

  • 如果未找到端点或参数,规则将输出警告并保持规范不变,以便及时更新 openapi-modifier 配置
  • 该规则可应用于任何类型的参数(query、path、header、cookie)

有用的链接