问题
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:
Go to apistudio.io:
- Insert -> New Model.
- CutNpaste your JSON.
- [The Swagger YML file will be generated]
- Download -> YAML.
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