0

I currently have my solution set up to produce Swagger documentation for each end point. However I have several end points that are only available for admins. Down below you will be able to see an example.

A regular user can create models, however only an admin can pull every single model in the database.

The challenge is to generate 2 sets of swagger documentation? One for regular users to see, and another piece of documentation for Admin users to see. I know that if I add [ApiExplorerSettings(IgnoreApi = true)] to my end point it will not appear in the documentation generated however this would mean that my admin users wont be able to see that vital piece of documentation as well. Any recommendation on how to dynamically generate two sets of documents depending on the user will help.

[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[HttpPost("/v1/packages")]
[Authorize()]
public async Task<IActionResult> CreateModel([FromBody]Request request)
{
    ...
}

The method below is for admins only:

[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[ApiExplorerSettings(IgnoreApi = true)]
[HttpPost("/v1/packages")]
[Authorize()]
public async Task<IActionResult> GetAllModelsFromDatabase([FromBody]Request request)
{
    ...
}
Lostaunaum
  • 697
  • 1
  • 10
  • 31

1 Answers1

0

A dynamic process was found in this answer.

Dynamically Ignore WebAPI method on controller for api explorer documentation

It is possible to separate swagger documents however there is no built in method to do this. One would have to remove the un wanted nodes from the one documentation file: https://github.com/swagger-api/swagger-editor/issues/233

This works fine in current editor if you host the editor yourself so parameters_common.yaml path can get resolved as an HTTP path. Currently there is no way to jump between files or create a new one. If you are doing a big project with Swagger, I recommend hosting the editor yourself. When editor and the API you are building are on the same origin, XHR call don't have to be cross-origin which help editor to show more details about calls in "try-operation" and your API doesn't have to have cross origin headers.

Example on how to split swagger file into smaller nodes. http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html

Lostaunaum
  • 697
  • 1
  • 10
  • 31