Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenApi Swashbuckle support for Aspnetcore APIs in F# #14392

Open
jkone27 opened this issue Nov 25, 2022 · 3 comments
Open

OpenApi Swashbuckle support for Aspnetcore APIs in F# #14392

jkone27 opened this issue Nov 25, 2022 · 3 comments
Labels
Area-Compiler-CodeGen IlxGen, ilwrite and things at the backend Feature Request Needs-RFC
Milestone

Comments

@jkone27
Copy link
Contributor

jkone27 commented Nov 25, 2022

Unable to use swashbuckle with F# minimal APIs (or Giraffe, or Saturn, or Falco or any other web framework)

https://www.youtube.com/watch?v=HXHwtEjQoyM&ab_channel=dotnet

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/openapi?view=aspnetcore-7.0

I tried this in F# and doesnt work.

Describe the solution you'd like

I would love to be able to just use openapi in F# aspnetcore projects and in any F# web framework wrapping around aspnetcore..

Describe alternatives you've considered

At the moment there is no alternative or no working alternative, openapi spec needs to be written by hand, making development and integration with other teams slower and possibly more error prone... and making F# sound like a "less professional choice" as it cannot easilly interact with common API tooling for .NET (swashbuckle nuget package).

Example of code that should work based on Aspnetcore docs

open System
open System.Collections.Generic
open System.IO
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.HttpsPolicy
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging
open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.OpenApi
open Swashbuckle.AspNetCore
open Microsoft.OpenApi

module Program =
    

    [<EntryPoint>]
    let main args =

        let builder = WebApplication.CreateBuilder(args)

        builder.Services.AddEndpointsApiExplorer() |> ignore
        builder.Services.AddSwaggerGen() |> ignore
        
        let app = builder.Build()

        if (app.Environment.IsDevelopment()) then
            app.UseSwagger() |> ignore
            app.UseSwaggerUI(fun c -> ()) |> ignore

        app.MapGet("/hello", 
            requestDelegate = fun x ->  task { return "hello" } )
            .WithName("HelloF#")
            .WithOpenApi()  |> ignore

        app.Run()
        0

but this gives me wrong api (hello returns an empty body, not "hello" text string..) , 200 OK but no content!

image

plus also the swagger api is empty
image

image

@T-Gro
Copy link
Member

T-Gro commented Nov 28, 2022

aspnetcore minimal APIs use several idioms which do not make their usage F# friendly as is.

A working alternative is (or at least used to be last time I tried) regular aspnetcore with class-based controllers and their members.

@T-Gro
Copy link
Member

T-Gro commented Nov 28, 2022

Relevant to fsharp/fslang-suggestions#984

@NinoFloris
Copy link
Contributor

There is nothing really wrong with F# and minimal apis, the main problem here is that the RequestDelegate overload does not add openapi info (which is by design atm, see dotnet/aspnetcore#44970).

You have to use the Delegate overload to get the 'true' minimal api experience. Additionally to make this easy in F# you probably want to add Func<T1,T2,T3,TEtc> extension methods for MapGet, MapPost etc (which do nothing but cast to Delegate and pass it into the main overload). fsharp/fslang-suggestions#1131 was opened to track that usability issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-CodeGen IlxGen, ilwrite and things at the backend Feature Request Needs-RFC
Projects
Status: New
Development

No branches or pull requests

4 participants