如何以xml格式制作可重用的示例

时间:2019-10-26 07:28:47

标签: xml swagger

我正在尝试以xml和json格式显示一些可重用的响应示例

我尝试写两种类型的响应示例,但是有一个错误

grid_values = {'C':[0.01, 0.1, 1, 10, 100]}

我想像这样:

responses:
   '400':  
          description: Ошибки валидации запроса  
          content:   
            application/xml:  
              schema:  
                $ref: '#/components/schemas/ErrorResponse'  
              examples:  
                400wrongBodyFormat:  
                  $ref: '#/components/examples/400wrongBodyFormat'

examples:  
  400wrongBodyFormat:  
        value:   
          httpCode: 400
          httpMessage: Bad Request
          moreInformation: Body of the request is not valid according to json 
                           schema

schemas:
    ErrorResponse:
      type: object
      properties:
        httpCode:
          type: integer
        httpMessage:
          type: string
        moreInformation:
          type: integer 

但是得到这个

<400>
  <httpCode>400</httpCode>
  <httpMessage>Bad request</httpMessage>
</400>

1 个答案:

答案 0 :(得分:0)

将XML响应示例指定为文本:

components:
  examples:  
    400wrongBodyFormat:  
      value: |-
        <?xml version="1.0"?>
        <ErrorResponse>
          <httpCode>400</httpCode>
          <httpMessage>Bad Request</httpMessage>
          <moreInformation>Body of the request is not valid according to json schema</moreInformation>
        </ErrorResponse>

您还可以从响应中删除examples,而是将属性示例值添加到架构,并让Swagger UI根据架构自动生成响应示例。

      responses:
       '400':  
          description: Ошибки валидации запроса  
          content:   
            application/xml:  
              schema:  
                $ref: '#/components/schemas/ErrorResponse'  

components:
  schemas:
    ErrorResponse:
      type: object
      properties:
        httpCode:
          type: integer
          example: 400
        httpMessage:
          type: string
          example: Bad Request
        moreInformation:
          type: integer
          example: Body of the request is not valid according to json schema