Swagger is a simple yet powerful representation of your RESTful API. With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability.
Swagger is very powerful framework that is able to generate schema and rich UI for your RESTful API. As I know, Swagger is very popular framework especially in the non-.NET world. You probably have already seen Swagger UI (like this) at several resources.
It turns out that it is not hard to use Swagger for .NET F# apps. There is a project called Swashbuckle, which adds Swagger to WebApi projects.
First of all, you need to create F# ASP.NET Web API project. You can do it using “F# Web Application templates (MVC 5 and Web API 2.2) by Ryan Riley and Daniel Mohl“. Choose “Web API 2.2 and Katana 3.0” option in project creation wizard.
So, now you have F# Web App with RESTful API: CarsController with two services ‘/api/cars‘ and ‘/api/car/{id}‘. Everything is awesome but we do not have UI that is able to show a list of available services, their parameters and return types. Swashbuckle will help us here, we need to install ‘Swashbuckle.Core‘ package to our web app.
Install-Package Swashbuckle.Core
The last step is to update HttpConfiguration in Startup.fs in proper way to register Swagger. Add following three lines to the end of RegisterWebApi method.
open Swashbuckle.Application type Startup() = static member RegisterWebApi(config: HttpConfiguration) = // ... // Swagger configuration config .EnableSwagger(fun c -> c.SingleApiVersion("v1", "My API") |> ignore) .EnableSwaggerUi();
That’s all! When you start your web application and open ‘/swagger/ui/index‘ URI you will see beautiful documentation for your RESTful API.
Would be cool to have a TypeProvider for Swagger some day!
Found it: http://sergey-tihon.github.io/SwaggerProvider/ 🙂
=) It is a prototype (Type Inference only)
FYI – I had problems with this on .NET 4.5, but was able to fix them by using “Install-Package Swashbuckle.Core.Net45”.
Hi Sergey,
I’m learning F# to potentially give myself some new career options. Something that drives me nuts about the whole C# web stack is how little it supports open generics out-of-the-box. Swagger generates documentation based on type composition, as opposed to function composition. Same thing with how Razor works. You cannot create a Razor view on an open generic type like List. Same thing with how most MVC model validation works – you cannot bind a “ModelValidator where T : new, MyModelBase” but you can bind a ModelValidator where MyModel is-a MyModelBase. Not sure if this interests you like it does me, but the lack of handling of open generics (while admittedly unsolvable in a general sense) makes writing some terse, but easy to understand, boilerplate code really hard.
Hi John, how can I help with your generics investigation journey?
Hey, thank you for this post, it is almost helpful to me.
Having a slightly newer project, I am missing a “Startup” class, so for the life of me I cannot figure out how to add access the HttpConfiguration.
Just use getting started manual from newer package https://github.com/domaindrivendev/Swashbuckle.AspNetCore
Thank you for your replies… Much awesome.
I apologies for the double/spam comment, I took a while for the initial to reflect, and then once I saw both I could not figure out how to remove one.
The link and description are helpful 🙂
Thank you for this post.
It is almost helpful to me.
I am having trouble that I have a slightly newer project structure, so I have no Startup Class.
For the life of me I cannot figure out how to access HttpConfiguration