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

Add ability to Configure Endpoints via IEndpointConventionBuilder #599

Merged
merged 3 commits into from
Jul 5, 2024

Conversation

mrtz-j
Copy link
Contributor

@mrtz-j mrtz-j commented Jun 16, 2024

Description

This PR adds the ability to configure endpoints via the IEndpointConventionBuilder. This would enable us to reuse some of the built-in OpenApi generation to generate OpenApi schemas from code. It's possible by replacing the MetadataList with a ConfigureEndpoint, which can be utilized to capture additional information about the endpoint.

I've taken inspiration from Oxpeckers Routing which uses a similar approach.

How to test

A working example provided in the Giraffe.OpenApi repo, which utilizes this branch. It's based on the MinimalApi approach, with a more functional syntax.

|> configureEndpoint _.WithName("Endpoint")
|> withOpenApi<unit, string>

In the example Swagger has been used to display the configured endpoints.

Endpoints can be added to the OpenApi documentation as follows:

We have the type FsharpMessage, with XML-documentation:

/// <summary>
/// Fsharp Message type 
/// </summary>
type FsharpMessage = {
    /// <summary>
    /// Hello content
    /// </summary>
    /// <example>This is an Example</example>
    Hello: string
}

and the endpoints /hello

let endpoints = [
      GET [
            route "/hello" (json {Hello = "Hello from Giraffe"})
            |> configureEndpoint _.WithTags("helloGiraffe")
            |> configureEndpoint _.WithSummary("Fetches a Hello from Giraffe")
            |> configureEndpoint _.WithDescription("Will return a Hello from Giraffe.")
            |> addOpenApiSimple<unit, FsharpMessage>
]

which will be produce the view below:

Endpoint and Type in Swagger

image

And for extra references the whole app.

Swagger view of the endpoints.

image

If it's accepted, I'll either publish Giraffe.OpenApi or make another MR for it here.

Related issues

@mrtz-j mrtz-j marked this pull request as ready for review June 16, 2024 12:16
@nojaf
Copy link
Contributor

nojaf commented Jun 17, 2024

Hello, thank you for this PR!

I can't review the code as I'm not suited enough for that.
However, this looks interesting. If this gets accepted would you be ok with Giraffe.OpenApi being part of the organization here? Seems like an interesting addition to the family. Just thinking out loud though.

//cc @64J0, @dbrattli

@mrtz-j
Copy link
Contributor Author

mrtz-j commented Jun 17, 2024

Hello, thank you for this PR!

I can't review the code as I'm not suited enough for that. However, this looks interesting. If this gets accepted would you be ok with Giraffe.OpenApi being part of the organization here? Seems like an interesting addition to the family. Just thinking out loud though.

//cc @64J0, @dbrattli

Hi there,

Yes, I would be fine with it being part of the organization.

@64J0
Copy link
Member

64J0 commented Jun 20, 2024

Hi, thanks for the PR, seems pretty cool. I think I'll have some time to review it next week!

@voronoipotato
Copy link

Fantastic, looking at it I don't see any issues. Since this works it would mean a lot to many of us if this was merged.

@dbrattli
Copy link
Contributor

dbrattli commented Jun 28, 2024

Thanks for submitting this RP. I think this looks really great 😊 We should also update the documentation before merging. I'm trying to figure out if this is a breaking change or not i.e for applications upgrading Giraffe to a newer version of the library. The addMetadata function has the same signature but still has changed to a more/less "curried" form, but the original is still curried so should this matter?

val addMetadata:
   metadata: obj ->
   endpoint: Endpoint
          -> Endpoint

to

val addMetadata:
   metadata: obj
          -> Endpoint -> Endpoint

I don't think it's a breaking change 🙈 but I'll let someone who knows F# better than me decide if it makes sense to keep the exact same signature just to be sure i.e change to:

let addMetadata (metadata: obj) (endpoint: Endpoint)=
        endpoint |> configureEndpoint _.WithMetadata(metadata)

@64J0 64J0 requested review from dbrattli and 64J0 June 28, 2024 16:58
@64J0
Copy link
Member

64J0 commented Jun 28, 2024

Since it's dealing with a piece of code similar to what we have on Oxpecker, I'll ping @Lanayx so he knows about it too

@64J0
Copy link
Member

64J0 commented Jun 28, 2024

Tested the sample application locally, and it's working as described. Great PR!

Regarding the addMetadata signature change pointed by @dbrattli, I agree that it's not a breaking change. I also agree that it would be nice to have the documentation updated. Would you like to do it @mrtz-j?

@mrtz-j
Copy link
Contributor Author

mrtz-j commented Jun 28, 2024

I've updated the addMetadata function to match the previous signature just in case 😅. I'd also be up for writing some documentation over the weekend, but it's also fine if one of you guys wants to.

Copy link
Member

@64J0 64J0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add the documentation later with another PR.

Btw, what I have in mind is:

  • Some paragraphs explaining how to use this new feature;
  • A new sample project.

@nojaf
Copy link
Contributor

nojaf commented Jul 4, 2024

Thanks for the review @64J0. Can we ship this as an alpha?

@64J0
Copy link
Member

64J0 commented Jul 4, 2024

Thanks for the review @64J0. Can we ship this as an alpha?

Sure @nojaf! I'll do it either today or tomorrow.

Copy link
Contributor

@dbrattli dbrattli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, and should be a pretty safe change since there's no breaking change and all the configure endpoint stuff is ASP specific anyways. The PR just enables you to configure it.

@64J0 64J0 merged commit 264a710 into giraffe-fsharp:master Jul 5, 2024
3 checks passed
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants