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

Autopatch not working when a prefix is present in the path #713

Open
froz42 opened this issue Jan 23, 2025 · 0 comments
Open

Autopatch not working when a prefix is present in the path #713

froz42 opened this issue Jan 23, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@froz42
Copy link

froz42 commented Jan 23, 2025

When a prefix is present in the path, the autopatch feature does not work as expected.
A 404 error occurs when a PATCH request is sent to the server.

Minimal Reproductible Example

func main() {
    router := chi.NewRouter()
    router.Route("/api", apiMux())
    err = http.ListenAndServe(fmt.Sprintf(":%d", 8080), router)
}

// apiMux returns a function that initializes the API routes
func apiMux() func(chi.Router) {
        return func(router chi.Router) {
            humaConfig := huma.DefaultConfig("API", "dev")
            humaConfig = openapi.WithAuthSchemes(humaConfig)
            humaConfig = openapi.WithOverviewDoc(humaConfig)
            humaConfig = openapi.WithServers(humaConfig, config)
            api := humachi.New(router, humaConfig)
            huma.Register(api, huma.Operation{
                Method:      "GET",
                Path:        "/ressources/{id}",
            }, getRessourceByID)
            huma.Register(api, huma.Operation{
                Method:      "PUT",
                Path:        "/ressources/{id}",
            }, updateRessourceByID)
            autopatch.AutoPatch(api)
    }
}

Technical details

The probleme is here:

origReq, err := http.NewRequest(http.MethodGet, ctx.URL().Path, nil)

Since ctx.URL().Path give the path with the prefix, for example /api/ressources/1, the /api prefix stay in the path given for the request, and the router does not find the route:

adapter.ServeHTTP(origWriter, origReq)

Possible Solution

I made an MR showing a potential solution here #714

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants