You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于OpenApiDocument生成API请求字符串的工具及实现方案咨询

基于OpenApiDocument生成API测试请求字符串的方案

一、手动实现排列组合生成

你可以直接解析OpenApiDocument的结构,结合参数的必填/可选属性、min/max值,生成所有合法请求字符串的排列组合,完全适配NUnit的TestCaseSource需求。

核心步骤:

  1. 提取API操作与参数
    遍历swaggerJson.Paths中的每个端点,针对目标HTTP方法(比如GET)提取对应的OpenApiOperation,再从Operation.Parameters中拆分出必填参数(Required = true)和可选参数。

  2. 生成参数的测试值集合
    对每个参数(无论必填/可选),根据其Schema的Minimum和Maximum属性生成测试值列表:

    • 数值型参数:至少包含min和max两个值
    • 若参数有其他约束(比如枚举),可补充对应值
    • 可选参数额外增加“不包含该参数”的选项
  3. 计算笛卡尔积生成所有组合

    • 先计算必填参数的所有值组合(每个必填参数选min或max,取笛卡尔积)
    • 再将每个必填组合与可选参数的所有可能组合(包含/不包含+值)进行二次笛卡尔积
    • 最后把组合后的参数拼接成完整的请求URL字符串

代码示例(适配NUnit)

public static IEnumerable<string> GenerateGetPositiveRequests()
{
    // 替换成你获取OpenApiDocument的逻辑
    var swaggerJson = GetTargetOpenApiDocument();

    foreach (var pathItem in swaggerJson.Paths)
    {
        var endpoint = pathItem.Key;
        var getOperation = pathItem.Value.Get;
        if (getOperation == null) continue;

        // 拆分必填与可选参数
        var requiredParams = getOperation.Parameters.Where(p => p.Required).ToList();
        var optionalParams = getOperation.Parameters.Where(p => !p.Required).ToList();

        // 生成必填参数的可选值(min/max)
        var requiredParamOptions = requiredParams.Select(p => 
        {
            var schema = p.Schema;
            var values = new List<string>();
            if (schema.Minimum != null) values.Add(schema.Minimum.ToString());
            if (schema.Maximum != null) values.Add(schema.Maximum.ToString());
            return values.Distinct().Select(val => $"{p.Name}={val}");
        });

        // 计算必填参数的所有组合
        var requiredCombinations = CartesianProduct(requiredParamOptions)
            .Select(c => string.Join("&", c));

        // 生成可选参数的所有选项:不包含、用min、用max
        var optionalParamOptions = optionalParams.Select(p => 
        {
            var schema = p.Schema;
            var options = new List<string> { "" };
            if (schema.Minimum != null) options.Add($"{p.Name}={schema.Minimum}");
            if (schema.Maximum != null) options.Add($"{p.Name}={schema.Maximum}");
            return options.Distinct();
        });

        // 计算可选参数的所有组合(排除全不选的空值)
        var optionalCombinations = CartesianProduct(optionalParamOptions)
            .Select(c => string.Join("&", c))
            .Where(s => !string.IsNullOrEmpty(s));

        // 合并必填与可选组合,生成最终请求字符串
        foreach (var requiredComb in requiredCombinations)
        {
            // 仅包含必填参数的请求
            yield return $"{endpoint}?{requiredComb}";

            // 包含必填+任意可选参数组合的请求
            foreach (var optionalComb in optionalCombinations)
            {
                yield return $"{endpoint}?{requiredComb}&{optionalComb}";
            }
        }
    }
}

// 辅助方法:计算多个集合的笛卡尔积
private static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
    IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
    foreach (var sequence in sequences)
    {
        result = from seq in result
                 from item in sequence
                 select seq.Concat(new[] { item });
    }
    return result;
}

二、工具库方案

如果不想手动实现,可尝试以下方向:

  • OpenAPI Generator:支持通过定制模板生成NUnit测试代码,可配置生成基于min/max的参数组合用例,但需要调整模板适配你的需求。
  • RestAssured.Net:可结合其参数构建能力,搭配自定义的参数组合逻辑生成请求,不过仍需自己实现排列组合的核心逻辑。

注意:目前没有完全贴合你需求的开箱即用库,因为参数组合规则(仅取min/max、必填+可选的组合)比较定制化,手动实现反而更灵活可控。


内容的提问来源于stack exchange,提问作者PanBocian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:25:54