【问题标题】:OpenAPI Spec Connection RefusedOpenAPI 规范连接被拒绝
【发布时间】:2017-06-20 13:48:57
【问题描述】:

我的 OpenAPI 规范文件有问题。我试图调用一个暴露的 url 来“获取”一个 id,但是每次我将服务转发到我的本地然后尝试通过 API 文档发送请求时,我的连接都被拒绝了。我将不胜感激任何帮助。我期望的 id 将采用 JSON 格式。以下是我的规范文件

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Id Generator
servers:
   url: www.someurl.com
paths:
  /posts:
    get:
      summary: Get Id
      operationId: id
      tags:
        - posts
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
              $ref: "#/definition/Post"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
              $ref: "#/definition/Error"

definition:
    Post:
    type: object 
    properties:
        id:
          type: string
    Error:
      properties:
        id:
          type: string

【问题讨论】:

  • 您如何进行 API 调用 - 从浏览器中的 Swagger UI 或其他方式?
  • 顺便说一下,您的规范不是有效的 3.0.0 或 2.0 规范。缩进在几个地方是错误的,并且规范使用了错误的 2.0 和 3.0.0 关键字。
  • @Helen 我正在使用 swagger UI 进行 API 调用。我没有收到 Visual Studio 代码的缩进错误。
  • @Helen 我做了一些更正以减轻 2.0 和 3.0 的混合。我在上面重新发布了我的规范。由于我直接将其粘贴到此处,因此可能存在缩进问题。但我在 Visual Studio 代码上没有任何缩进错误。我仍然无法使用我的规范文件成功获得响应。
  • 我是openapi新手,想问一下openapi yaml文件是怎么得到的?生成了吗?

标签: swagger swagger-2.0 openapi


【解决方案1】:

截至 2017 年 6 月 21 日,OpenAPI 规范 3.0 尚未发布,并且 Swagger UI 还不支持 OpenAPI 3.0,因此您的示例可能无法运行。密切关注Swagger UI releases 页面,了解何时支持 OpenAPI 3.0。

此外,您需要修复规范中的错误以使其成为有效的 OpenAPI 3.0 规范:

  • servers 是一个数组,因此将其更改为:

    servers:
      - url: http://www.someurl.com
    
  • 必须引用响应状态代码:"200"'200'

  • 在架构下缩进$ref

                  schema:
                    $ref: "#/definition/Post"
                  ...
                  schema:
                    $ref: "#/definition/Error"
    
  • definition 更改为components->schemas 并修复Post 的缩进:

    components:
      schemas:
        Post:
          type: object 
          properties:
            id:
              type: string
        Error:
          properties:
            id:
              type: string
    

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多