我正在为具有OpenAPI(Swagger)定义的REST API构建一个模糊器。
我想测试OpenAPI定义中的所有可用路径,生成数据以测试服务器,分析响应代码和内容,以及验证响应是否符合API定义。
我正在寻找一种从模型定义生成数据(JSON对象)的方法。
例如,给定此模型:
...
"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"
}
}
}
我想生成随机数据并得到类似的结果:
{
"id": 0,
"category": {
"id": 0,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "string"
}
答案 0 :(得分:2)
我的经历:
简而言之:基于Swagger定义生成客户端(在我的情况下是java-client),填充它的模型并编组结果。
答案 1 :(得分:1)
Swagger Inflector库具有ExampleBuilder
类,正是为此目的。它使您可以从OpenAPI(Swagger)定义中的模型生成JSON,XML和YAML示例。
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);
以上示例使用Swagger Java库1.x,该库支持OpenAPI 2.0定义(swagger: '2.0'
)。
如果您的API定义是OpenAPI 3.0(openapi: 3.0.0
),则需要使用Swagger Java库的2.x版本,并相应地更新导入和类名,例如io.swagger.parser.SwaggerParser
→io.swagger.v3.parser.OpenAPIV3Parser
等
答案 2 :(得分:0)
只需将您的模型放入 https://json-schema-faker.js.org/ 即可。
您提供的架构只需稍作修改即可正常工作:删除“宠物”并添加“类别”和“标签”的定义。请参阅下面的示例。
然后点击“生成”,你会得到假数据。如果您不想浏览网站,这似乎都可以通过库以编程方式完成(不过我自己还没有尝试过)。
{
"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"
}
}
}