Repository navigation
Conversation
236c7ef to
f419fb7
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #775 +/- ##
==========================================
- Coverage 94.72% 94.71% -0.02%
==========================================
Files 25 25
Lines 5193 5202 +9
==========================================
+ Hits 4919 4927 +8
- Misses 269 270 +1
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Actually, now that I look at the code, it looks like I've pushed a second take at this. The new one uses the first parameter to |
|
Hey @costela! Thanks for this contribution, and sorry that it hasn't been reviewed yet! I'm still working on getting up-to-speed with some of the more complex parts of the codebase, but want to try and resolve issues and merge PRs where feasible to do so. This PR may require @danielgtaylor's review, but in the meantime I'd appreciate it if you could resolve any merge conflicts :) |
This allows exposing the spec and docs on a separate adapter, potentially under a separate path and/or middleware stacks.
E.g.:
```
opts := huma.DefaultConfig("foo", "0.0.1")
opts.DocsAdapter = humachi.NewAdapter(internalRouter)
opts.CreateHooks = []func(huma.Config) huma.Config{
huma.DefaultSchemaLinkHook(huma.DefaultSchemaRefPrefix, internalPrefix), // assuming internalRouter above is mounted under internalPrefix
}
adapter := humachi.NewAdapter(mainRouter)
huma.NewAPI(opts, adapter)
```
Make sure that the main adapter does not serve the docs and the spec when DocsAdapter is set. Also make sure that the docs link to the spec with a relative URL, because the server prefix applies only to the main adapter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dbc2946 to
d89c772
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Documentation and schema URL handling can produce broken links or ignore explicit mount prefixes, while the hook-loop change can panic on shortened slices and skip newly appended hooks.
4 open issues
What changed in this PR
This PR adds an optional documentation adapter, allowing OpenAPI specifications, documentation, and schemas to use a separate router, mount path, or middleware stack from API endpoints.
Changes:
- Routes documentation-related endpoints through
DocsAdapterwhen configured. - Extracts a reusable schema-link hook and adds schema mount-prefix handling.
- Adds tests for documentation and specification routing on a separate adapter.
| File | Description |
|---|---|
transforms.go |
Adds a schema-path prefix fallback to link generation. |
defaults.go |
Exports the schema reference prefix and extracts DefaultSchemaLinkHook. |
api.go |
Adds documentation adapter routing and adjusts spec URLs and hook iteration. |
api_test.go |
Tests separate-adapter routing and generated documentation links. |
adapters/humachi/humachi_test.go |
Adds a commented-out schema-link hook example; no test behavior changes. |
Review effort: Balanced
| // api := huma.NewAPI(config, adapter) | ||
| func NewAPI(config Config, a Adapter) API { | ||
| for i := 0; i < len(config.CreateHooks); i++ { | ||
| for i := range config.CreateHooks { |
|
|
||
| if config.SchemasPath != "" { | ||
| a.Handle(&Operation{ | ||
| docsAdapter.Handle(&Operation{ |
| // The API prefix describes the main adapter, not the docs adapter, so | ||
| // link to the spec relative to the docs page instead. | ||
| docsAdapter = a.config.DocsAdapter | ||
| openAPIPath = strings.TrimPrefix(openAPIPath, "/") |
| if apiPrefix := getAPIPrefix(oapi); apiPrefix != "" { | ||
| schemasPath = path.Join(apiPrefix, schemasPath) | ||
| } else if t.apiPrefix != "" { | ||
| schemasPath = path.Join(t.apiPrefix, schemasPath) | ||
| } |

This allows exposing the spec and docs on a separate adapter, potentially under a separate path and/or middleware stacks.
E.g.: (assuming
internalRouterandmainRouterare twochi.Router)This is a bit of a WIP:
getAPIPrefixcall in the stoplight template is really as harmless as I thinkgetAPIPrefixcall will currently break the schema links if the API is mounted somewhere other than the root.But regardless of the limitations above: WDYT about the general idea?