【问题标题】:JSON schema for openapi3openapi3 的 JSON 模式
【发布时间】:2019-11-22 20:47:32
【问题描述】:

什么是 OpenAPI (https://www.openapis.org/) v3 的 JSON 架构参考?

例如,如果我想将一些模式定义为 JSON,我可以这样做:

{
  "$schema": "http://json-schema.org/schema#",
  // rest of the document will need to conform to the referenced schema
}

如何以类似的方式引用 OpenAPI 3? 我的意思是,我可以这样写:

{
"$schema": "http://<some-url-for-openapi-v3>",
}

并且通过这种方式强制文档符合模式并允许一些高级 JSON 编辑器在编辑期间提供自动完成功能?

这个 some-url-for-openapi-v3 是什么?

v2 和 v3 的示例位于: https://github.com/OAI/OpenAPI-Specification/tree/master/examples 但是在写作的时候 v2 只有 yaml 示例,并且没有一个(json 或 yaml)有类似架构引用的东西。

有没有可能实现?

【问题讨论】:

    标签: jsonschema openapi


    【解决方案1】:
    1. 在 OpenApi 中,您需要定义路径、方法、响应、组件和模式。此外,您甚至可以引用这样的架构:
    paths:
      /users:
        post:           
          parameters:
            - in: path
              name: users
              required: true
              schema:
                type: string
                example: users
          responses:
            200:
              description: Success Response
              content:
                application/json; charset=utf-8:
                  schema:
                    $ref: '#/components/schemas/SuccessResponce'
    
    
    Components:
      schemas:
        SuccessResponce:
          description: 'Successful response'
          type: string
    
    1. 著名的 Swagger Petstore example 也回答了您的问题。

    2. 请浏览 OpenApi 规范here

    【讨论】:

      猜你喜欢
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2016-12-21
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      相关资源
      最近更新 更多