Documenting roles with swashbuckle

How to document attribute usage with swagger

You could basically document any attribute you have decorated your actions with, but this will focus mainly on documenting the role part of the Authorize attribute.
When using roles based authentication I like to document the roles in my swagger spec, this gives a nice indication, to the consumer, of which roles are required for different endpoints.
Here I have a basic Authorize attribute with a required role of “Admin”.

As with most other things swashbuckle related, we need to create an IOperationFilter

And that is it, you have now auto documented every role required across your API.
The swagger docs will look like the following.
BackupPortal_API

Throttling actions in WebApi using AttributeFilter

I recently had to come up with a solution to throttle an action in an API i’ve written.
First thing I did was to see what others had used (No reason to invent the wheel right?).
I came across this stackoverflow answer – which is basically what I will use in this blog post (mostly for a reminder to myself I guess).
Full credits go to Jarrod Dixon

The Attribute

The Annotation

Since it’s an attribute, we just annote it to an action
The attribute takes a unique name, amount of seconds and the message to return.
If the action is run more often than allowed, the action will return 409 - conflict

Remove 'Try it out!' buttons from Swagger-ui

This is a quick way of removing the ‘try it out’ buttons from specific operation types, when using Swagger-ui for documentation

This will be a short one.
When using Swashbuckle to create documentation, you might want to remove the Try it out buttons, maybe all of them, or maybe just the ones on Put on Post operations
Well I’ve created a small script that is injected into swagger-ui through the SwaggerConfig.cs
The following script will remove the TryItOut buttons from all Put and Post operations.

The script is injected through the SwaggerConfig.cs like so

Remember that the .js file needs to be an Embedded Resource else it won’t work.