【问题标题】:Validate OpenAPI response in java在 Java 中验证 OpenAPI 响应
【发布时间】:2021-12-13 23:12:54
【问题描述】:

我正在使用 Java 进行 OpenAPI 3.x 测试,我想测试并验证来自服务器的响应。

到目前为止,我发现有一个库 Rest Assured 可用于 API 测试。我设法测试和验证我的简单响应,例如使用 JUnit 测试获取状态。

我发送一个 GET 请求并检查状态代码和内容类型等... 但我也想检查服务器的响应是否有正确的答案和数据类型。以我在 .yml 中的 OpenAPI 规范为例:

    "class_name": "8A",
    "count": 4,
    "students_in_class": [
        {
            "id": "s0001",

            "first_name": "Jack",
            "last_name": "London",
            "gender": "male",
            "age": 10,
            "marks": {
                "mathematics": 70,
                "science": 84
            }
        },

在来自服务器的响应正文中,我想检查我的响应的数据类型,例如我想查看“first_name”是否确实是 String 类型,而“age”是否是 Integer 类型。

这是否可以在 Java 中使用 Rest Assured 或者是否有任何其他方法可以在 Java 中测试 OpenAPI,因为我想在 Gitlab 管道 CD/CI 中连接 API 测试的测试,以便我始终检查我的 OpenAPI 是否良好。

希望有人能回答我。

【问题讨论】:

    标签: java validation swagger openapi rest-assured


    【解决方案1】:

    将此库添加到您的项目中,然后断言 json 架构。

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>4.3.1</version>
    </dependency>
    

    将架构放入文件中:例如api-schema.json

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "class_name": {
          "type": "string"
        },
        "count": {
          "type": "integer"
        },
        "students_in_class": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "id": {
                  "type": "string"
                },
                "first_name": {
                  "type": "string"
                },
                "last_name": {
                  "type": "string"
                },
                "gender": {
                  "type": "string"
                },
                "age": {
                  "type": "integer"
                },
                "marks": {
                  "type": "object",
                  "properties": {
                    "mathematics": {
                      "type": "integer"
                    },
                    "science": {
                      "type": "string"
                    }
                  },
                  "required": [
                    "mathematics",
                    "science"
                  ]
                }
              },
              "required": [
                "id",
                "first_name",
                "last_name",
                "gender",
                "age",
                "marks"
              ]
            }
          ]
        }
      },
      "required": [
        "class_name",
        "count",
        "students_in_class"
      ]
    }
    

    然后用matchesJsonSchemaInClasspath()方法验证它

    import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
    
    given()...
    .then().body(matchesJsonSchemaInClasspath("api-schema.json"));
    

    【讨论】:

    • 但是我在 .yml 文件中有我的 OpenAPI 文档。一切都在那里。这是否意味着我必须在 .json 文件中全部重写?不确定到底该怎么做@lucasguyen17
    • 我不知道有什么方法可以用一个文件 yml 来验证每个响应。这似乎是不可能的。所以我的解决方案是一次将每个响应与相应的 json 模式进行匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多