FSharp.SystemTextJson icon indicating copy to clipboard operation
FSharp.SystemTextJson copied to clipboard

Support OpenApi Schema generation

Open Swoorup opened this issue 5 years ago • 2 comments

It would be nice if we could add OpenApi 3.0 schema generation. So F# types could directly be exposed in REST models. Its possible to override schema using SchemaFilter in Swashbuckle.

Swoorup avatar Feb 22 '20 14:02 Swoorup

I am hacking out a initial implementation like this:

        interface ISchemaFilter with
            member this.Apply (schema: OpenApiSchema, context: SchemaFilterContext) =
                let objType = context.Type
                if objType |> IsUnion then
                    schema.Properties <- Dictionary()
                    
                    let cases =
                        GetUnionCases objType
                        |> Seq.map (fun field ->
                            let case = OpenApiSchema()
                            case.Title <- field.Key
                            case
                            )
                        |> BCLConverters.seqToList
                    
                    let obj = OpenApiSchema()
                    obj.Type <- "object"
                    obj.OneOf <- cases
                    
                    let caseProp = OpenApiSchema(Type = "string")
                    
                    schema.Properties.Add("Case", caseProp)
                    schema.Properties.Add("Fields", obj)
                    ()
                

Which generates the following: image

However the code is duplicated. Any ideas so it can be unified across the serializer as well as openapi generation?

Swoorup avatar Feb 27 '20 00:02 Swoorup

@Swoorup maybe it should go to Microsoft.OpenAPI package?

xperiandri avatar Aug 21 '22 15:08 xperiandri