使用Swashbuckle.Swagger手动添加端点

使用Swashbuckle.Swagger手动添加端点

问题描述:

我正在使用CMS.因此,当我转到"/painter"时,其路由到"JobController". /plumber也被路由到'JobController'. 除此之外,它是MVC而不是WebAPI,因此大张旗鼓不会发现它,这是可以理解并且很好的.

I'm using a CMS. So when I go to i.e. '/painter' its routed to the 'JobController'. /plumber is also routed to 'JobController'. Besides that it's MVC and not WebAPI, so swagger doesn't discover it, which is understandable and fine.

但是我有一个用例,如果我访问/pianter?json = 1,它将返回json而不是HTML.

But I've a usecase, where if I access /pianter?json=1 it returnes json instead of HTML.

因此,作为API UI,我们希望公开此假"端点,以便设计人员可以看到输出模型.

So as an API UI we would like to expose this 'fake' endpoint, just so the designers can see the output model.

那么我可以添加一个完全伪造的端点吗?只是为了在庞大的UI中的设计人员和开发人员之间使用单个用户界面?

So can I add an entirely fake endpoint - just to have a single userinterface between the designers and developers in swagger UI?

除了具有可视化的UI外,我们还希望基于openapi标准生成一些TypeScript.

Besides having a visual UI, we want to generate some TypeScript based on the openapi standard.

以下是使用IDocumentFilter使用swashbuckle创建Fake端点的选项:

Here is an option to create Fake endpoint with swashbuckle using IDocumentFilter:

    private class DocumentFilterAddFakes : IDocumentFilter
    {
        private PathItem FakePathItem(int i)
        {
            var x = new PathItem();
            x.get = new Operation()
            {
                tags = new[] { "Fake" },
                operationId = "Fake_Get" + i.ToString(),
                consumes = null,
                produces = new[] { "application/json", "text/json", "application/xml", "text/xml" },
                parameters = new List<Parameter>()
                            {
                                new Parameter()
                                {
                                    name = "id",
                                    @in = "path",
                                    required = true,
                                    type = "integer",
                                    format = "int32"
                                }
                            },
            };
            x.get.responses = new Dictionary<string, Response>();
            x.get.responses.Add("200", new Response() { description = "OK", schema = new Schema() { type = "string" } });
            return x;
        }

        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            for (int i = 0; i < 10; i++)
            {
                swaggerDoc.paths.Add("/Fake/" + i + "/{id}", FakePathItem(i));
            }
        }
    }

这是类似这样的结果: http://swashbuckletest.azurewebsites.net/swagger/ui/index#/Fake

Here is the result of something like that: http://swashbuckletest.azurewebsites.net/swagger/ui/index#/Fake

其后的完整代码在github上: https://github.com/heldersepu/SwashbuckleTest/blob /master/Swagger_Test/App_Start/SwaggerConfig.cs#L316

The full code behind that is on github: https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs#L316