使用Swashbuckle Asp.Net Core for ReDoc添加x徽标供应商扩展 [英] Adding x-logo vendor extension using Swashbuckle Asp.Net Core for ReDoc

查看:202
本文介绍了使用Swashbuckle Asp.Net Core for ReDoc添加x徽标供应商扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swagger.json文件(由Swashbuckle生成)供ReDoc使用,以显示API文档.

I'm using swagger.json file (generated by Swashbuckle) for ReDoc to display API documentation.

我需要什么:x-logo供应商扩展名添加到使用Swashbuckle(Swashbuckle.AspNetCore.SwaggerGen库)生成的swagger json中,以便ReDoc UI在左上角显示徽标

What I Need: Add x-logo vendor extension to swagger json generated using Swashbuckle (Swashbuckle.AspNetCore.SwaggerGen library) so that ReDoc UI shows logo at the top left corner like this

问题: 我能够将x-log添加到swagger.json文件,但是将其添加到文件的错误部分.它必须在info部分中.

Problem: I was able to add x-log to the swagger.json file but it is added to wrong section of the file. It needs to be inside info section.

这就是我添加x-logo

  • 创建了如下所示的文档过滤器
    public class XLogoDocumentFilter : IDocumentFilter
        {
            public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
            {
              swaggerDoc.Extensions["x-logo"] = new { url = "https://URL/of/the/logo", altText = "Company Logo" };
            }

        }

  • 将过滤器添加为SwaggerDoc
    • Added the filter to SwaggerDoc as
    • services.AddSwaggerGen(options => 
      {   
        options.DocumentFilter<XLogoDocumentFilter>();
      
      });
      

      实际

          {
            "swagger": "2.0",
            "info": {
              "version": "v1",
              "title":"Sample REST API"
            },
            "x-logo": {
              "url": "https://rebilly.github.io/ReDoc/petstore-logo.png",
              "altText": "Aimia Logo"
            }
          }
      

      预期

          {
            "swagger": "2.0",
            "info": {
              "version": "v1",
              "title":"Sample REST API",
              "x-logo": {
                "url": "https://rebilly.github.io/ReDoc/petstore-logo.png",
                "altText": "Aimia Logo"
              }
            },
      
          }
      

      非常感谢在swagger.json文件的正确部分包含x-logo的任何帮助或建议.

      Really appreciate any help or suggestions to have the x-logo in the correct section of the swagger.json file.

      推荐答案

      输入问题后,我自己找到了解决方案.与其直接将扩展名添加到swaggerDoc,不如将扩展名添加到swaggerDoc.Info对象.

      After typing the question I found the solution myself. Instead of adding extension directly to swaggerDoc, add it to swaggerDoc.Info object.

      public class XLogoDocumentFilter : IDocumentFilter
              {
                  public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
                  {   
                    // need to check if extension already exists, otherwise swagger 
                    // tries to re-add it and results in error  
                    if (!swaggerDoc.Info.Extensions.ContainsKey("x-logo"))
                    {
                       swaggerDoc.Info.Extensions.Add("x-logo", new {
                          url = "https://URL/To/The/Logo",
                          altText = "Logo",
                      });
                    }           
      
                  }
      
              }
      

      这篇关于使用Swashbuckle Asp.Net Core for ReDoc添加x徽标供应商扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆