The Swagger Inflector library has the ExampleBuilder class exactly for this purpose. It lets you generate JSON, XML and YAML examples from models in an OpenAPI (Swagger) definition.
OpenAPI 2.0 example
To work with OpenAPI 2.0 (swagger: '2.0') definitions, use Swagger Java libraries 1.x.
import io.swagger.parser.SwaggerParser;
import io.swagger.models.*;
import io.swagger.inflector.examples.*;
import io.swagger.inflector.examples.models.Example;
import io.swagger.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import java.util.Map;
import com.fasterxml.jackson.databind.module.SimpleModule;
...
// Load your OpenAPI/Swagger definition
Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");
// Create an Example object for the Pet model
Map<String, Model> definitions = swagger.getDefinitions();
Model pet = definitions.get("Pet");
Example example = ExampleBuilder.fromModel("Pet", pet, definitions, new HashSet<String>());
// Another way:
// Example example = ExampleBuilder.fromProperty(new RefProperty("Pet"), swagger.getDefinitions());
// Configure example serializers
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
Yaml.mapper().registerModule(simpleModule);
// Convert the Example object to string
// JSON example
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);
// YAML example
String yamlExample = Yaml.pretty().writeValueAsString(example);
System.out.println(yamlExample);
// XML example (TODO: pretty-print it)
String xmlExample = new XmlExampleSerializer().serialize(example);
System.out.println(xmlExample);
OpenAPI 3.0 example
For an OpenAPI 3.0 example, see this answer. You need version 2.x of Swagger Java libraries, and update the imports and class names appropriately, e.g. change io.swagger.parser.SwaggerParser to io.swagger.v3.parser.OpenAPIV3Parser and so on.
How to generate JSON examples from OpenAPI/Swagger model definition? - Stack Overflow
How to generate a Swagger #definition from sample JSON - Stack Overflow
How do you create Swagger jsons for your API?
Swagger for Django api
Videos
The Swagger Inflector library has the ExampleBuilder class exactly for this purpose. It lets you generate JSON, XML and YAML examples from models in an OpenAPI (Swagger) definition.
OpenAPI 2.0 example
To work with OpenAPI 2.0 (swagger: '2.0') definitions, use Swagger Java libraries 1.x.
import io.swagger.parser.SwaggerParser;
import io.swagger.models.*;
import io.swagger.inflector.examples.*;
import io.swagger.inflector.examples.models.Example;
import io.swagger.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.util.Json;
import io.swagger.util.Yaml;
import java.util.Map;
import com.fasterxml.jackson.databind.module.SimpleModule;
...
// Load your OpenAPI/Swagger definition
Swagger swagger = new SwaggerParser().read("http://petstore.swagger.io/v2/swagger.json");
// Create an Example object for the Pet model
Map<String, Model> definitions = swagger.getDefinitions();
Model pet = definitions.get("Pet");
Example example = ExampleBuilder.fromModel("Pet", pet, definitions, new HashSet<String>());
// Another way:
// Example example = ExampleBuilder.fromProperty(new RefProperty("Pet"), swagger.getDefinitions());
// Configure example serializers
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
Yaml.mapper().registerModule(simpleModule);
// Convert the Example object to string
// JSON example
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);
// YAML example
String yamlExample = Yaml.pretty().writeValueAsString(example);
System.out.println(yamlExample);
// XML example (TODO: pretty-print it)
String xmlExample = new XmlExampleSerializer().serialize(example);
System.out.println(xmlExample);
OpenAPI 3.0 example
For an OpenAPI 3.0 example, see this answer. You need version 2.x of Swagger Java libraries, and update the imports and class names appropriately, e.g. change io.swagger.parser.SwaggerParser to io.swagger.v3.parser.OpenAPIV3Parser and so on.
Just put your model in https://json-schema-faker.js.org/ and off you go.
Your provided schema works as is with minor modifications: remove "Pets" and add definitions for "Category" and "Tag". See below as an example.
Then click "Generate" and you get fake data back. It appears that this can all be done programmatically via the libraries if you don't want to go through the website (haven't tried that myself though).
{
"definitions": {
"description": "making this up so all refs resolve",
"Category": {
"type": "string"
},
"Tag": {
"type": "string"
}
},
"comment": "From here on down, it's exactly the same as the OP schema",
"type": "object",
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"$ref": "#/definitions/Category"
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store"
}
}
}
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.
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/