Giraffe icon indicating copy to clipboard operation
Giraffe copied to clipboard

Naming parameters in endpoint path

Open pir2o61 opened this issue 1 year ago • 3 comments

It seems that there is no way to name parameters in path (mainly for use in Swagger). Something like {petId:%i} in https://github.com/hvester/EndpointBuilder will be very nice. image

One way that it can be done is via changing convertToRouteTemplate function in EndpointRouting. Something like below should be enough:

            | '%' :: c :: tail ->
                let (placeholderName, placeholderTemplate), newTail = 
                    match tail with
                    | ':' :: stail ->
                        let splitIndex = stail |> List.tryFindIndex (fun c -> c = '/')
                        match splitIndex with
                        | Some splitIndex ->
                            let name , newTail = 
                                stail 
                                |> List.splitAt splitIndex
                            getConstraint i c (Some (System.String.Concat(Array.ofList(name)))), newTail
                        | None -> 
                            getConstraint i c (Some (System.String.Concat(Array.ofList(stail)))), []
                    | _ ->
                        getConstraint i c None, tail
                let template, mappings = convert (i + 1) newTail
                placeholderTemplate + template, (placeholderName, c) :: mappings

where getConstraint is changed like this

    let private getConstraint (i : int) (c : char) (name : string option) =
        let name = Option.defaultValue (sprintf "%c%i" c i) name
  1. Do we want this feature?
  2. On what scope it should work? (what names do we allow, do we want to have it in format %c:name. `name:%c' or different

pir2o61 avatar Aug 11 '24 21:08 pir2o61

Do we want this feature?

Yes. We want this feature. Otherwise, the Giraffe.OpenApi feels kind of incomplete since the path parameters are just interpreted as i1, s2, etc.

I'm assuming this feature is related to https://github.com/giraffe-fsharp/Giraffe.OpenApi/issues/6 ?

On what scope it should work? (what names do we allow, do we want to have it in format %c:name. `name:%c' or different?)

I think [type]:[name] would be alright. In Oxpecker, they also support the ASP.NET route constraints, thus the name is derived from the handler parameter type and the route looks like [type]:[constraint].

Not suggesting we change things drastically in Giraffe to conform to Oxpecker's way of doing things, just bringing it up for comparison.

RJSonnenberg avatar May 15 '25 20:05 RJSonnenberg

Do you think we can close this issue after this PR is merged?


Update: Giraffe version 8.0.0-alpha-003 was just released. This new version contains the changes added by the PR mentioned before.

64J0 avatar Jun 01 '25 19:06 64J0

I think this can be closed now. @pir2o61 , what do you think?

RJSonnenberg avatar Jun 10 '25 16:06 RJSonnenberg

Closing this issue, since there were no new interactions for a while, and I think that the PRs mentioned before already cover its goal. Feel free to open a new issue later if this is not the case.

64J0 avatar Jul 07 '25 21:07 64J0