Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate JSON examples from OpenAPI/Swagger model definition?

I'm building a fuzzer for a REST API that has an OpenAPI (Swagger) definition.

I want to test all available path from the OpenAPI definition, generate data to test the servers, analyse responses code and content, and to verify if the responses are conform to the API definition.

I'm looking for a way to generate data (JSON object) from model definitions.

For example, given this model:

...
"Pet": {
  "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"
    }
  }
}

I want to generate random data and get something like this:

{
  "id": 0,
  "category": {
    "id": 0,
    "name": "string"
  },
  "name": "doggie",
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "string"
}
like image 905
Youssouf Maiga Avatar asked Dec 31 '16 13:12

Youssouf Maiga


2 Answers

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.

like image 90
Helen Avatar answered Sep 20 '22 08:09

Helen


My experience:

  1. go to http://editor.swagger.io
  2. File -> Import file (to load my own Swagger description)
  3. Generate client -> Java (in my case)
  4. Download and extract the client
  5. Import it's model package into any simple project, instantiate and fill model's classes with the data you need
  6. Marshall the instantiated objects into the JSON (My case - Gson, because the generated model is annotated by Gson annotations)
  7. Profit

In short: generating client (java-client in my case) based on Swagger definition, filling it's model and marshalling the result.

like image 28
Yury Shaban Avatar answered Sep 22 '22 08:09

Yury Shaban