Swashbuckle 对于大型模式来说非常慢

Swashbuckle very slow for large schema

我有一个包含 90 多个表的数据库,这些表都通过外键相互交叉链接。

当我打开 API 的 swagger 页面时,加载需要超过 2 分钟。原因似乎是它正在为每个 API 生成 ModelExample Value。由于交叉链接,几乎 每个 实体都引用每个其他实体(通过递归传递闭包)!

当 swagger 生成 ModelExample Value[= 时,有什么方法可以 禁用或限制 递归29=]?

例如,

class A {
   int id;
   List<B> Blist;
}

class B {
   int id;
   List<C> Clist;
}

class C {
   int id;
   List<D> Dlist;
}

/// etc...

如果我有 GET /api/A 的 API 那么我不想将所有 类 拉入 swagger 页面 Model .太大了!!我只想拉A.

这是一个似乎 "fix" 它的技巧。我将此添加到我的 SwaggerConfig.cs

  c.MapType<MasterModel>(() => new Schema { type = "integer", format = "int32" });
  c.MapType<MasterLocationModel>(() => new Schema { type = "integer", format = "int32" });
  c.MapType<LocationModel>(() => new Schema { type = "integer", format = "int32" });

我还有很多类型,但这三个是核心类型,因此通过将它们映射到整数,它可以将递归限制在合理的水平。它也使 swagger 页面出错,但至少它加载了!

也许有一种方法可以使用 SchemaFilter 更好地做到这一点?任何帮助将不胜感激。

编辑:

最后,我们决定拆分 类,这样 swagger 就无法看到超过一层深度的引用。例如,

class A_Base {
   int id;
}

class A : A_Base {
   List<B_Base> Blist;
}

class B_Base {
   int id;
}

class B : B_Base {
   List<C_Base> Clist;
}

class C_Base {
   int id;
}

class C : C_Base {
   List<D_Base> Dlist;
}

和我们所有的控制器 API 都使用类型 A、B、C。swagger 文档只会深入一层。递归并没有疯狂地失控。而且它更正确,因为我们的 API 返回的对象通常只有零或一级深度。

您问题的答案很简短

Is there any way to limit the recursion when swagger generates the Model and Example?

很遗憾没有!

目前没有这样的选项。但这是可能的,我们正在讨论:
https://github.com/swagger-api/swagger-ui/issues/4411
在那里添加评论并为该问题 +1,让团队知道这对您很重要。

Swagger-UI 不能很好地处理复杂的模式,有时速度很慢,有时会导致浏览器崩溃,团队知道,希望我们能尽快得到修复。

现在您的版本 (2.x) 不再受支持,因此不会有修复...
你试过Swagger-Net了吗?那是我的叉子,我使用的是最新版本的 UI