9

我正在为具有 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"
}
4

3 回答 3

8

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.

于 2018-07-02T10:20:16.873 回答
2

我的经验:

  1. http://editor.swagger.io
  2. 文件 -> 导入文件(加载我自己的 Swagger 描述)
  3. 生成客户端-> Java(在我的情况下)
  4. 下载并解压客户端
  5. 将它的模型包导入到任何简单的项目中,实例化并用您需要的数据填充模型的类
  6. 将实例化的对象编组到 JSON 中(我的案例 - Gson,因为生成的模型由 Gson 注释进行注释)
  7. 利润

简而言之:根据 Swagger 定义生成客户端(在我的例子中为 java-client),填充它的模型并编组结果。

于 2017-07-12T09:19:36.493 回答
1

只需将您的模型放入https://json-schema-faker.js.org/即可。

您提供的架构只需稍作修改即可正常工作:删除“Pets”并添加“Category”和“Tag”的定义。请参阅下面的示例。

然后单击“生成”,您将获得虚假数据。如果您不想浏览网站,这一切似乎都可以通过库以编程方式完成(虽然我自己没有尝试过)。

{
  "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"
    }
  }
}
于 2020-12-20T02:18:03.013 回答