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

查看:256
本文介绍了将JSON转换为与Swagger 2.0兼容的JSON Schema draft 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/#/给我带来了什么:

But, what http://jsonschema.net/#/ gives me:

---
"$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.

Schema Object ...基于JSON Schema Specification Draft 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 draft 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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