将 JSON 转换为与 Swagger 2.0 兼容的 JSON Schema 草案 4 [英] Convert JSON to JSON Schema draft 4 compatible with Swagger 2.0

查看:19
本文介绍了将 JSON 转换为与 Swagger 2.0 兼容的 JSON Schema 草案 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了一些由具有大量属性的 REST API 生成的 JSON 文件.

I've been given some JSON files generated by a REST API with plenty of properties.

我为此 API 创建了 Swagger 2.0 定义,需要为其提供相应的响应架构.

I've created a Swagger 2.0 definition for this API and need to give it the corresponding schema for the response.

主要问题:这个 JSON 文件有很多属性.如果我手动编写模式,这将花费很多时间,而且我会犯很多错误.这不是我需要描述的唯一 API.

The main problem: this JSON file has loads of properties. It would take so much time and I would make many mistakes if I write the schema manually. And it’s not the only API I need to describe.

我知道有一些工具可以将 JSON 转换为 JSON 模式,但是,如果我没记错的话,Swagger 只有 $refs 到其他对象定义,因此只有一个级别,而我发现的工具只生成树结构模式.我的问题:是否有任何工具可以将 JSON(或 JSON Schema)转换为与 Swagger 2.0 兼容的工具?

I know there are some tools to convert JSON to JSON schemas but, if I’m not mistaken, Swagger only has $refs to other objects definitions thus only has one level whereas the tools I’ve found only produce tree structured schemas. My question: is there any tool to convert a JSON (or JSON Schema) to a Swagger 2.0 compatible one ?

注意:我正在使用 YAML,但我不会成为问题,不是吗?

Note: I'm working in YAML but I wouldn't be an issue, would it ?

例如,我需要什么:

  List of Movements:
    type: "array"
    items:
      $ref: "#/definitions/Movement"
  Movement:
    properties:
      dateKey:
        type: "string"
      movement:
        $ref: "#/definitions/Stock"
    additionalProperties: false
  Stock:
    properties:
      stkUnitQty:
        type: "string"
      stkDateTime:
        type: "string"
      stkUnitType:
        type: "string"
      stkOpKey:
        type: "string"
    additionalProperties: false

对于我的 JSON 文档:

For my JSON document:

[
  {
    "dateKey": "20161110",
    "stkLvls": [
      {
        "stkOpKey": "0",
        "stkUnitType": "U",
        "stkDateTime": "20161110T235010.240+0100",
        "stkUnitQty": 30
      }
    ]
  },
  {
    "dateKey": "20161111",
    "stkLvls": [
      {
        "stkOpKey": "0",
        "stkUnitType": "U",
        "stkDateTime": "20161111T231245.087+0100",
        "stkUnitQty": 21
      }
    ]
  }
  ]

但是,http://jsonschema.net/#/ 给了我什么:

---
"$schema": http://json-schema.org/draft-04/schema#
type: array
items:
  type: object
  properties:
    dateKey:
      type: string
    stkLvls:
      type: array
      items:
        type: object
        properties:
          stkOpKey:
            type: string
          stkUnitType:
            type: string
          stkDateTime:
            type: string
          stkUnitQty:
            type: integer
        required:
        - stkOpKey
        - stkUnitType
        - stkDateTime
        - stkUnitQty
  required:
  - dateKey
  - stkLvls

我是新手,但很好奇,请不要犹豫深入解释.

I'm new to that, but curious, don't hesitate to explain deeply.

提前感谢您的帮助!

推荐答案

我知道有一些工具可以将 JSON 转换为 JSON 模式,但是,如果我没记错,Swagger 对其他对象定义只有 $refs因此只有一层

I know there are some tools to convert JSON to JSON schemas but, if I’m not mistaken, Swagger only has $refs to other objects definitions thus only has one level

你错了.Swagger 将尊重任何有效的 v4 JSON 模式,只要它只使用 支持的子集.

You are mistaken. Swagger will respect any valid v4 JSON schema, as long as it only uses the supported subset.

模式对象...基于 JSON 模式规范草案 4 和使用它的预定义子集.在这个子集之上,本规范提供了一些扩展,以允许更完整文档.

The Schema Object...is based on the JSON Schema Specification Draft 4 and uses a predefined subset of it. On top of this subset, there are extensions provided by this specification to allow for more complete documentation.

接着列出了 JSON 模式中支持的部分,不支持的位,以及被 swagger 扩展的位.

It goes on to list the parts of JSON schema which are supported, and the bits which are not, and the bits which are extended by swagger.

这篇关于将 JSON 转换为与 Swagger 2.0 兼容的 JSON Schema 草案 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆