【问题标题】:How to hide the Models section in Swagger UI?如何隐藏 Swagger UI 中的模型部分?
【发布时间】:2019-07-22 02:39:53
【问题描述】:

我使用Swagger UI 来显示 API 文档。默认情况下,它会在底部显示“模型”部分:

如何隐藏?

【问题讨论】:

  • 任何人都可以提供直接的招摇 UI 隐藏修复?

标签: swagger-ui


【解决方案1】:

要隐藏“模型”部分,请将 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 文档呈现。

【讨论】:

【解决方案2】:

对于 .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");
});

【讨论】:

    【解决方案3】:

    在 Docket bean 中添加 新的

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

    希望对你有帮助

    【讨论】:

      【解决方案4】:

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

      SWAGGER_SETTINGS = {
          'DEFAULT_MODEL_DEPTH':-1
      }
      

      【讨论】:

        【解决方案5】:

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

        <?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 公开的道具。 希望这对某人有帮助:)

        【讨论】:

        • 这不是问题要问的。
        • 这就是我在回答的第一句话中提到的!但最终结果与原始问题所寻找的相同:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-01
        • 2015-02-18
        • 1970-01-01
        • 2019-02-08
        相关资源
        最近更新 更多