How to generate a Swagger #definition from sample JSON

泪湿孤枕 提交于 2020-06-25 02:04:56

问题


Take the following #definition from the pet store example. Given a #definition section a JSON structure can be generated

e.g.

Is there something that can do the reverse given a largeish complex JSON file?

Given the below JSON Structure can I get the #defintion section of a swagger file generated to save some typing

{
  "variable": "sample",
  "object1": {
    "obj-field1": "field 1 of object",
    "obj-field2": "field 2 of object",
    "anArray": [
      "Value 1",
      {
        "anArrayObj1": "obj1fieldinarray",
        "anArrayObj2": "obj2fieldinarray"
      }
    ]
  }
}

回答1:


You can use this JSON-to-OpenAPI schema converter:
https://roger13.github.io/SwagDefGen/

(GitHub project)

I haven't used it personally though, so I'm not sure how good it is.


Since OpenAPI uses a subset of JSON Schema, you could also use one of the JSON Schema generators, however you may need to manually tweak the generated definition to make it OpenAPI-compatible.




回答2:


1 - Paste a response in http://www.mocky.io and get a link to your response

2 - Go to https://inspector.swagger.io/ and make a call to your example response

3 - Select the call from "History" and click "Create API definition"

4 - The swagger definition will be available at https://app.swaggerhub.com/




回答3:


This works for me:

Generate Swagger REST-client code (and POJO) from sample JSON:

  1. Go to apistudio.io:

    • Insert -> New Model.
    • CutNpaste your JSON.
    • [The Swagger YML file will be generated]
    • Download -> YAML.
  2. Go to editor.swagger.io:

    • CutNpaste the YML saved from last step.
    • Generate Client -> jaxrs-cxf-client (there are many other options).


来源:https://stackoverflow.com/questions/49277137/how-to-generate-a-swagger-definition-from-sample-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!