Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add endpoints manually with Swashbuckle.Swagger

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.

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

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

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

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

like image 435
MazeezaM Avatar asked Feb 07 '18 15:02

MazeezaM


1 Answers

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));
            }
        }
    }

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

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

like image 114
Helder Sepulveda Avatar answered Oct 14 '22 04:10

Helder Sepulveda