“variable in openapi 3.0 response schema” 可以翻译为 “OpenAPI 3.0 响应模式中的变量”。

huangapple go评论49阅读模式
英文:

variable in openapi 3.0 response schema

问题

Here's the translated code portion you provided:

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /myaction/{action}s:
    get:
      summary: 获取特定操作的信息
      operationId: 获取操作信息
      parameters:
        - name: action
          in: path
          required: true
          description: 操作的名称
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                    description: 响应对象的名称
                  {action}:
                    type: string
                    description: 操作的名称
                  {action}Id:
                    type: integer
                    description: 操作的ID
                required:
                  - name
                  - {action}
                  - {action}Id

Please note that I've translated the comments and descriptions to Chinese, but the code structure remains the same.

英文:

I have a legacy REST endpoint, which I would like to adress using openapi 3.0.

The Endpoint uses a path variable:

path: action/{action}s/

and also uses this path variable in the response schema like:

response in json:

{
  "name": "someName",
  "{action}": "someActionName",
  "{action}Id": 5
}

I couldn't find a explaination about this in the openapi guide. Is it possible to generate an openapi 3.0 spec with such a request? If yes, how would this example look like?

with this:

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /myaction/{action}s:
    get:
      summary: Get information for a specific action
      operationId: getActionInfo
      parameters:
        - name: action
          in: path
          required: true
          description: The name of the action
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                    description: The name of the response object
                  {action}:
                    type: string
                    description: The name of the action
                  {action}Id:
                    type: integer
                    description: The ID of the action
                required:
                  - name
                  - {action}
                  - {action}Id

in editor.swagger.io, I get a parser error in line 31. (The line with "{action}Id:")

答案1

得分: 2

看起来你正在尝试在你的模式中定义逻辑。OpenAPI 不支持根据用户定义的参数定义键的能力。最多,你可以使用 OpenAPI 的多态响应来定义基于用户输入的潜在响应。例如:

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /myaction/{action}s:
    get:
      summary: 获取特定动作的信息
      operationId: 获取动作信息
      parameters:
        - name: action
          in: path
          required: true
          description: 动作的名称
          schema:
            type: string
            enum:
              - create
              - delete
              - update
              - get
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                    description: 响应对象的名称
                oneOf:
                  - $ref: '#/components/schemas/CreateAction'
                  - $ref: '#/components/schemas/DeleteAction'
                  - $ref: '#/components/schemas/UpdateAction'
                  - $ref: '#/components/schemas/GetAction'
                required:
                  - name
components:
  schemas:
    CreateAction:
      type: object
      required:
        - create
        - createId
      properties:
        create:
          type: string
          description: 动作的名称
        createId:
          type: integer
          description: 动作的ID
    DeleteAction:
      required:
        - delete
        - deleteId
      type: object
      properties:
        delete:
          type: string
          description: 动作的名称
        deleteId:
          type: integer
          description: 动作的ID
    UpdateAction:
      type: object
      required:
        - update
        - updateId
      properties:
        update:
          type: string
          description: 动作的名称
        updateId:
          type: integer
          description: 动作的ID
    GetAction:
      type: object
      required:
        - get
        - getId
      properties:
        get:
          type: string
          description: 动作的名称
        getId:
          type: integer
          description: 动作的ID

这将允许你基于请求定义响应。如果选择这个方法,我强烈建议使用枚举作为动作,以确保只有正确的响应可以被发送回去。

然而,另一个选择是彻底改变响应。你可以做类似下面这样的事情,看起来更清晰,也更具可扩展性。

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /myaction/{action}s:
    get:
      summary: 获取特定动作的信息
      operationId: 获取动作信息
      parameters:
        - name: action
          in: path
          required: true
          description: 动作的名称
          schema:
            $ref: '#/components/schemas/Actions'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                    description: 响应对象的名称
                  action:
                    type: object
                    required:
                      - name
                      - type
                      - id
                    properties:
                      type:
                        $ref: '#/components/schemas/Actions'
                      name:
                        type: string
                        description: 动作的名称
                      id:
                        type: integer
                        description: 动作的ID
components:
  schemas:
    Actions:
      type: string
      description: 要执行的动作
      enum:
        - push
        - pull
        - punch
        - kick

这里需要注意的一点是,这只是一个例子。我不建议在响应中使用枚举,因为对响应的更改可能会破坏你的 API 的任何使用者。

英文:

It looks like you are trying to define logic within your schema. Openapi does not support the ability to define a key based upon a user defined parameter. At most, you could use openapi polymorphic responses to define a potential response based upon the user's input. For example:

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /myaction/{action}s:
    get:
      summary: Get information for a specific action
      operationId: getActionInfo
      parameters:
        - name: action
          in: path
          required: true
          description: The name of the action
          schema:
            type: string
            enum:
              - create
              - delete
              - update
              - get
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                    description: The name of the response object
                oneOf:
                  - $ref: '#/components/schemas/CreateAction'
                  - $ref: '#/components/schemas/DeleteAction'
                  - $ref: '#/components/schemas/UpdateAction'
                  - $ref: '#/components/schemas/GetAction'
                required:
                  - name
components:
  schemas:
    CreateAction:
      type: object
      required:
        - create
        - createId
      properties:
        create:
          type: string
          description: The name of the action
        createId:
          type: integer
          description: The ID of the action
    DeleteAction:
      required:
        - delete
        - deleteId
      type: object
      properties:
        delete:
          type: string
          description: The name of the action
        deleteId:
          type: integer
          description: The ID of the action
    UpdateAction:
      type: object
      required:
        - update
        - updateId
      properties:
        update:
          type: string
          description: The name of the action
        updateId:
          type: integer
          description: The ID of the action
    GetAction:
      type: object
      required:
        - get
        - getId
      properties:
        get:
          type: string
          description: The name of the action
        getId:
          type: integer
          description: The ID of the action

This will allow you to define the response based upon the request. If going this route, I highly suggest using an enum as the action, to ensure only the proper response can be sent back.

However, another option would be to change the response altogether. You could do something like this, and it would look cleaner and be more scalable.

openapi: 3.0.0
info:
  title: My API
  version: 1.0.0
paths:
  /myaction/{action}s:
    get:
      summary: Get information for a specific action
      operationId: getActionInfo
      parameters:
        - name: action
          in: path
          required: true
          description: The name of the action
          schema:
            $ref: '#/components/schemas/Actions'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  name:
                    type: string
                    description: The name of the response object
                  action:
                    type: object
                    required:
                      - name
                      - type
                      - id
                    properties:
                      type:
                        $ref: '#/components/schemas/Actions'
                      name:
                        type: string
                        description:  The name of the action
                      id:
                        type: integer
                        description: The ID of the action
components:
  schemas:
    Actions:
      type: string
      description: The action to perform
      enum:
        - push
        - pull
        - punch
        - kick

One thing of note here is that this is just an example. I don't recommend using enums in responses, as a change to your response could break any consumers of your api.

答案2

得分: 0

是的,所有版本的OpenAPI规范都支持部分段路径模板。

英文:

Yes, partial segment path templates are supported by all versions of the OpenAPI specification.

huangapple
  • 本文由 发表于 2023年5月11日 15:31:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定