Skip to content

Add Builder to simplify openapi document object construction #673

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

Open
jkone27 opened this issue Dec 23, 2021 · 4 comments
Open

Add Builder to simplify openapi document object construction #673

jkone27 opened this issue Dec 23, 2021 · 4 comments
Labels
help wanted type:enhancement Enhancement request targeting an existing experience
Milestone

Comments

@jkone27
Copy link

jkone27 commented Dec 23, 2021

A regular C# builder pattern would help a lot in many scenarios.
Building an object now is very complex...

Builder pattern is also the "de facto" in Aspnetcore.. so would make sense to expose a similar api

var openApiDocumentBuilder = new OpenApiDocumentBuilder()
     .WithServers(...)
     .WithOperation(...)
     .WithOperation(...)
     .WithSchema(...);
//... etc


var openApiDocument = openApiDocumentBuilder.Build();
     
@darrelmiller
Copy link
Member

Agreed, building it is clunky. Would you mind starting a discussion thread on ways that we could improve construction?

@jkone27
Copy link
Author

jkone27 commented Jan 24, 2022

I very nice project i saw in the F# space is this one (it uses F# Computation Expressions CEs for the builder pattern), but probably with extension methods something much similar can be obtained.. (or maybe also the F# part could be included in the "official one" for the F# api)

https://github.com/akhansari/FSharp.OpenApi

let document =
    apiDocument {
        info (apiInfo {
            version "1.0.0"
            title "Swagger Petstore (Simple)"
        })
        servers [
            apiServer {
                url "http://petstore.swagger.io/api"
            }
        ]
        paths [
            "/pets", apiPathItem {
                operations [
                    OperationType.Get, apiOperation {
                        description "Returns all pets from the system that the user has access to"
                        responses [
                            HttpStatusCode.OK, apiResponse {
                                description "OK"
                            }
                        ]
                    }
                ]
            }
        ]
    }

let outputString =
    document.Serialize (OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json)

i guess the C# counterpart would be like

var document = 
    OpenApi.Builder()
          .WithInfo(....)
          .WithServers(...)
          .WithPaths(...)
          .Build();
          
  var openApiJsonV3String = document.Serialize (OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
          

@captainsafia
Copy link
Member

I came across this issue while drafting a proposal for a new API in ASP.NET Core that builds on top of Microsoft.OpenAPI (see dotnet/aspnetcore#44192).

IMO, it would be really neat to have a builder-style pattern for an OpenAPI document.

@adhiambovivian adhiambovivian added the status:needs-discussion An issue that requires more discussion internally before resolving label Apr 3, 2023
@darrelmiller darrelmiller added help wanted and removed status:needs-discussion An issue that requires more discussion internally before resolving labels Apr 29, 2025
@darrelmiller
Copy link
Member

If someone wants to prototype a builder object, they are welcome to create a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted type:enhancement Enhancement request targeting an existing experience
Projects
None yet
Development

No branches or pull requests

4 participants