如何在 Swagger UI 中隐藏模型部分?

How to hide the Models section in Swagger UI?

我使用 Swagger UI 来显示 API 文档。默认情况下,它在底部显示 "Models" 部分:

如何隐藏?

要隐藏 "Models" 部分,请将 defaultModelsExpandDepth: -1 添加到 index.html 中的 Swagger UI 配置代码。

注意选项名称使用复数 Model*s* 而不是 Model.

// index.html

<script>
window.onload = function() {
  // Begin Swagger UI call region
  const ui = SwaggerUIBundle({
    url: "https://petstore.swagger.io/v2/swagger.json",
    dom_id: '#swagger-ui',
    defaultModelsExpandDepth: -1,   // <-------

Swagger UI 还有许多其他 configuration options 控制 API 文档渲染。

虽然不是您正在寻找的确切结果,但我发现通过以下方式禁用您不想要的模型的属性是完美的:

<?php
// src/AppBundle/Entity/User.php

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

...

 * @ApiResource(
 *     attributes={
 *         "normalization_context"={"groups"={"api"}},
 *         "denormalization_context"={"groups"={"api"}}
 *     },

...

 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"api","user:read"})
     */
    protected $id;

    /**
     * @var \DateTime
     */
    private $disabledProperty;

这样,您将获得一个模型,其中包含您通过群组 api 公开的道具。 希望这对某人有帮助:)

在 Docket bean 中添加 新

Docket(DocumentationType.SWAGGER_2).ignoredParameterTypes(YourClass.class,YourAnother.class)

希望对您有所帮助

对于 .Net Core 3.0 只需添加 c.DefaultModelsExpandDepth(-1);在你的 Startup.cs

// Startup.cs

app.UseSwaggerUI(c =>
{
    c.DefaultModelsExpandDepth(-1); // Disable swagger schemas at bottom
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API V1");
});

如果使用 Django,请将此添加到您的 settings.py:

SWAGGER_SETTINGS = {
    'DEFAULT_MODEL_DEPTH':-1
}