在 OpenAPI 中,我可以使用常规架构组件作为参数吗?

In OpenAPI, can I use a regular schema component as a parameter?

我在 API 中定义了一个带有路径参数的端点。

该参数的有效值已表示为枚举,我将其定义为架构组件并在响应中使用。

两个问题:

  1. 如果您可以只使用对该架构组件的引用作为参数,为什么要设置专用参数组件而不是使用可以在任何地方重复使用的通用架构组件?
  2. 如果您应该使用参数而不是通用模式,您可以创建一个引用模式组件的参数组件吗?我找不到任何通过验证的语法来执行此操作。

我想在这里粘贴我的定义,但我找不到任何不会造成可怕混乱的格式设置选项。

If you can just use a reference to that schema component as the parameter, why ever set up dedicated parameter components instead of using a generic schema component that you can re-use anywhere?

Parameter definitions have additional attributes not present in schemas, such as the parameter location in the request (in: path, in: query, etc.), serialization method 用于数组和对象值,以及其他。 schema 只是参数属性之一,但模式本身并不能提供足够的信息来有效地描述参数。

can you create a parameter component that refers to a schema component?

是的。参数有一个 schema,它可以是一个内联模式或一个 $ref:

paths:
  /something/{role}:
    get:
      parameters:
        - $ref: '#/components/parameters/role'
...

components:
  parameters:
    role:
      in: path
      name: role
      required: true
      schema:
        $ref: '#/components/schemas/UserRole'  # <-----

  schemas:
    UserRole:
      type: string
      enum: [user, admin]