且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

调用Swagger生成器插件

更新时间:2022-10-20 22:21:06

我找到了解决方法:



所需的所有元素都可以通过依赖项注入来检索,或者在启动时进行静态引用以保留它。像generatoroptions一样。
this.changeProvider = changeProvider;
this.schemaRegistryFactory = schemaRegistryFactory;
this.apiDescriptionGroupCollectionProvider = apiDescriptionGroupCollectionProvider;
this.mvcJsonOptionsAccessor = mvcJsonOptionsAccessor;
}

该方法的实现将如下所示:

  SwaggerDocument gen = new SwaggerGenerator(apiDescriptionGroupCollectionProvider,schemaRegistryFactory,Swagger.SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions).GetSwagger(version); 


var jsonBuilder = new StringBuilder();

var _swaggerSerializer = SwaggerSerializerFactory.Create(mvcJsonOptionsAccessor);
using(var writer = new StringWriter(jsonBuilder))
{
_swaggerSerializer.Serialize(writer,gen);
返回Ok(jsonBuilder.ToString());
}

这将适用于Swashbuckle.AspNetCore 4.0.1版本...即将结束的版本Swashbuckle.AspNetCore(5.x)由于必须支持openapi 2和3,因此必须具有另一个实现。


i want to generate the swagger document for plugins.

I point the endpoint for the api to a plugincontroller. In this i have a method to create the documentation for a particular version. While loading the plugin all items are already registered in the swagger tooling. (somehow the new documents don't get picked up by the swagger middleware that is why i need this workaround.)

  [HttpGet("api/plugins/swaggerdoc/{version}")]
        public IActionResult GetSwaggerDoc(string version)
        {
            SwaggerDocument gen = new SwaggerGenerator(apiDescriptionGroupCollectionProvider, schemaRegistryFactory, Swagger.SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions).GetSwagger(version);

            return Ok(gen);

        }

but it fails to generate the document properly. It shows to much information about the properties. e.g.

"parameters":[  
           {  
              "name":"api-version",
              "in":"query",
              "description":null,
              "required":false,
              "type":"string",
              "format":null,
              "items":null,
              "collectionFormat":null,
              "default":null,
              "maximum":null,
              "exclusiveMaximum":null,
              "minimum":null,
              "exclusiveMinimum":null,
              "maxLength":null,
              "minLength":null,
              "pattern":null,
              "maxItems":null,
              "minItems":null,
              "uniqueItems":null,
              "enum":null,
              "multipleOf":null
           }

how can i resolve this issue?

I found a solution:

All elements needed can be retrieved through dependency injection, or make a static reference in the startup to get a hold on it. Like generatoroptions.

  public PluginsController(IActionDescriptorChangeProvider changeProvider, IOptions<MvcJsonOptions> mvcJsonOptionsAccessor, ISchemaRegistryFactory schemaRegistryFactory, IApiDescriptionGroupCollectionProvider apiDescriptionGroupCollectionProvider)
    {
        this.changeProvider = changeProvider;
        this.schemaRegistryFactory = schemaRegistryFactory;
        this.apiDescriptionGroupCollectionProvider = apiDescriptionGroupCollectionProvider;
        this.mvcJsonOptionsAccessor = mvcJsonOptionsAccessor;
    }

the implementation of the method will be something like this:

            SwaggerDocument gen = new SwaggerGenerator(apiDescriptionGroupCollectionProvider, schemaRegistryFactory, Swagger.SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions).GetSwagger(version);


            var jsonBuilder = new StringBuilder();

            var _swaggerSerializer = SwaggerSerializerFactory.Create(mvcJsonOptionsAccessor);
            using (var writer = new StringWriter(jsonBuilder))
            {
                _swaggerSerializer.Serialize(writer, gen);
                return Ok(jsonBuilder.ToString());
            }

This will work for the 4.0.1 version of Swashbuckle.AspNetCore... The upcomping version of Swashbuckle.AspNetCore (5.x) will have to have another implementation because of the support of openapi 2 and 3.