danielgtaylor / danielgtaylor/huma

Route groups break docs

Open
#684 2 comments 1 reaction 0 assignees View on GitHub
question
Dominant language
Go
Stars
4.4k
Forks
285
Avg merge
40m
Merged PRs (30d)
1

Description

I followed the docs about [Route Groups & Base URLs](https://huma.rocks/features/bring-your-own-router/?h=group#route-groups-base-urls) to group my API under `/v1/sports`. I'm using Chi and identical structure to the example.

The relevant part of my code:

```go
app := NewApp(client)
api := API{app: app}

// Create a new router & API.
router := chi.NewMux()
router.Route("/v1/sports", func(router chi.Router) {
config := huma.DefaultConfig("My API", "1.0.0")
config.CreateHooks = nil // Do not add $schema links.
// config.OpenAPIPath = "/v1/sports/openapi"

routes := humachi.New(router, config)
// My routes.
})
chi.Walk(router, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
fmt.Printf("[%s]: '%s'\n", method, route)
return nil
})

```

It generates these routes:

```
...
[GET]: '/v1/sports/docs'
[GET]: '/v1/sports/openapi-3.0.json'
[GET]: '/v1/sports/openapi-3.0.yaml'
[GET]: '/v1/sports/openapi.json'
[GET]: '/v1/sports/openapi.yaml'
```

The problem is the docs HTML at `/v1/sports/docs` accesses the `openapi.yaml` using the following path: `/openapi.yaml` producing:

![image](https://github.com/user-attachments/assets/f5dfe211-21b6-4246-a0fa-1ed745629c8e)

Setting `OpenAPIPath` doesn't help. If I uncomment the following line:

```go
config.OpenAPIPath = "/v1/sports/openapi"
```

...this is the routes it generates:

```
...
[GET]: '/v1/sports/docs'
[GET]: '/v1/sports/v1/sports/openapi-3.0.json'
[GET]: '/v1/sports/v1/sports/openapi-3.0.yaml'
[GET]: '/v1/sports/v1/sports/openapi.json'
[GET]: '/v1/sports/v1/sports/openapi.yaml'
```

So now, when I'm accessing `/v1/sports/docs`, the error becomes:

![image](https://github.com/user-attachments/assets/7858ade4-2c45-4dc7-86f8-bba62b16ad8d)

It's because it expects the "correct" path based on the following code in `api.go`:

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the Route Groups & Base URLs example with Chi, then inspect api.go where the elements-api apiDescriptionUrl is assembled from openAPIPath. Check the generated routes and the docs request for /v1/sports/docs; done means the HTML requests the OpenAPI YAML at the correct route without a missing or duplicated /v1/sports prefix.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.