Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate JSON examples from OpenAPI 3.0 yaml file?

I have my openapi: 3.0.0 YAML file, I'm looking for a way to generate test data response (JSON object) from schema. This is what I am looking for, but I can't get it working for openapi: 3.0.0, the code works perfectly for "swagger": "2.0" definitions. I have tried to get the code working with Swagger Java libraries 2.x, which support OpenAPI 3.0. I know I need to use version 2.x of Swagger.

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);

// Convert the Example object to string

// JSON example
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);

This code is working, just need to get the same code working for openapi: 3.0.0.

like image 753
Nilz Avatar asked May 03 '19 23:05

Nilz


People also ask

Is OpenAPI a JSON?

OpenAPI Data Types. The specific format used to write OpenAPI documents is either YAML or JSON.

Is Swagger a JSON schema?

Lucas, Swagger uses an extended subset of JSON Schema do describe models. If you describe something outside that scope using JSON Schema, it will not be supported by Swagger.


2 Answers

Found the solution,

OpenAPI swagger = new OpenAPIV3Parser().read("url to Open API 3.0 Swagger")
Map < String, Schema > definitions = swagger.getComponents().getSchemas()
Schema model = definitions.get("Pet")
Example example = ExampleBuilder.fromSchema(model, definitions)
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer())
Json.mapper().registerModule(simpleModule)
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);
like image 91
Nilz Avatar answered Sep 28 '22 08:09

Nilz


import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.oas.inflector.examples.models.Example;
import io.swagger.oas.inflector.examples.ExampleBuilder;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.oas.inflector.processors.JsonNodeExampleSerializer;
import io.swagger.util.Json;

OpenAPI swagger = new OpenAPIV3Parser().read("C:\\Users\\ABC\\Downloads\\Petstore-1.0.yaml")
Map<String, Schema> definitions = swagger.getComponents().getSchemas();
Schema model = definitions.get("Pet");
Example example = ExampleBuilder.fromSchema(model, definitions);
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
String jsonExample = Json.pretty(example);
System.out.println(jsonExample);

Dependency for swagger inflector

compile group: 'io.swagger', name: 'swagger-inflector', version: '2.0.0'
like image 43
Ankit Kanani Avatar answered Sep 28 '22 08:09

Ankit Kanani