huma icon indicating copy to clipboard operation
huma copied to clipboard

Feature Request: Enable Optional Path Parameters

Open oliver-helix opened this issue 5 months ago • 1 comments

For our use case, we'd like to use optional path parameters. If the registered path does not include the path parameter, then the struct property can be an empty string.

Tested in a fork, and this works perfectly:

--- a/huma.go
+++ b/huma.go
@@ -119,6 +119,9 @@ func findParams(registry Registry, op *Operation, t reflect.Type) *findResult[*p
                        pfi.Loc = "path"
                        name = p
                        pfi.Required = true
+                       if r := f.Tag.Get("required"); r == "false" {
+                               pfi.Required = false
+                       }
                } else if q := f.Tag.Get("query"); q != "" {
                        pfi.Loc = "query"
                        split := strings.Split(q, ",")

oliver-helix avatar Jul 14 '25 21:07 oliver-helix

This would be very helpful, and should be a standard expectation in my opinion. For example: I want to build an endpoint that returns a list of files, if the call does not include an id, then it should return files in the root, and if the call does contain an id, then it should return files in that directory.

I tried `path:"id" required:"false"`, but this seems to be ignored, the API still returns status 422 with error required path parameter is missing. Also tried `path:"id,omitempty"`, even though it is not documented, just to see what happens, and indeed it just uses id,omitempty as the name.

So I guess the only workaround is to have two endpoints, one called "List files" and one called "List files in folder" which is pretty redundant.

thnee avatar Jul 17 '25 12:07 thnee