简体   繁体   中英

Customising swagger ui to separate documentation

I have integrated swagger into my Web API/OData project using swashbuckle 5.x and swashbuckle-Odata. If I navigate to http://root_url/swagger I can see all available API documentation in one big list. Every thing works perfectly fine but I have given a list of Odata controllers and API(s) which I need to show in a separate list . I know this is something which I need to do by create a custom index.html for swagger and inject into swaggerconfig.cs like

c.CustomAsset("index", thisAssembly, "SwaggerUI_Config.SwaggerExtensions.index.html");

I have been researching on internet on how I can separate swagger documentation so that I can create a different HTML list and acheive my result, so far no luck. Has anyone done something similar? Can you please give me some suggestions or pointer where I need to begin?.

I'm trying to achive following structure on my swagger documentation.

+ Custom API list
  +API Controller #1
   > GET API
   > POST API
   > PUT API
   > DELETE API
  +API Controller #2
   > GET API
   > POST API
   > PUT API
   > DELETE API

+ All available API(s)
  +API Controller #1
   > GET API
   > POST API
   > PUT API
   > DELETE API
  +API Controller #2
   > GET API
   > POST API
   > PUT API
   > DELETE API
  +API Controller #3
   > GET API
   > POST API
   > PUT API
   > DELETE API

I may have misconstrued your requirements but you should be able to group your actions using the GroupActionsBy method in your EnableSwagger call in SwaggerConfig.cs:

c.GroupActionsBy(apiDesc =>
{
    string controllerName = apiDesc.ActionDescriptor.ControllerDescriptor.ControllerName;
    string method = apiDesc.ActionDescriptor.SupportedHttpMethods.First().Method;

    return string.Format("{0} {1} API", controllerName, method);
}

You might want to use @Api annotation with tags attribute provided by Swagger. It will organize your APIs on swagger UI dashboard the way you want. Eg

@Path("apiController1")
@Api(value = "/apiController1", tags = "API CONTROLLER 1")
@Produces({
        MediaType.APPLICATION_JSON
})
public class APIController1 {

    @GET
    @Path("fooGet")
    public final String fooGet() {
        return "Hello, World";
    }
}

In some other controller.

@Path("apiController2")
@Api(value = "/apiController2", tags = {
        "API CONTROLLER 2", "DEFAULT"
})
@Produces({
        MediaType.APPLICATION_JSON
})
public class APIController2 {

    @GET
    @Path("fooGet2")
    public final String fooGet2() {
        return "Hello, World";
    }
}

and the Swagger UI Dashboard will look like this. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM