Skip to content

proposal: support docs on separate adapter #775

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

costela
Copy link
Contributor

@costela costela commented Mar 21, 2025

This allows exposing the spec and docs on a separate adapter, potentially under a separate path and/or middleware stacks.

E.g.: (assuming internalRouter and mainRouter are two chi.Router)

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)

This is a bit of a WIP:

  • I'm not 100% sure the removal of getAPIPrefix call in the stoplight template is really as harmless as I think
  • the other remaining getAPIPrefix call 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?

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)
```
@costela costela force-pushed the support-docs-on-separate-adapter branch from 236c7ef to f419fb7 Compare March 21, 2025 22:50
Copy link

codecov bot commented Mar 21, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 93.06%. Comparing base (4977a7a) to head (dbc2946).

Files with missing lines Patch % Lines
transforms.go 33.33% 2 Missing and 2 partials ⚠️
api.go 75.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #775      +/-   ##
==========================================
- Coverage   93.10%   93.06%   -0.04%     
==========================================
  Files          23       23              
  Lines        5296     5295       -1     
==========================================
- Hits         4931     4928       -3     
- Misses        313      314       +1     
- Partials       52       53       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@costela
Copy link
Contributor Author

costela commented Mar 25, 2025

Actually, now that I look at the code, it looks like NewSchemaLinkTransformer's prefix parameter isn't even being used? 🙈

I've pushed a second take at this. The new one uses the first parameter to NewSchemaLinkTransformer, so it may be considered a breaking change?
Also I've tried to keep respecting the prefix detection via the OpenAPI.Servers, though I cannot find any place in the documentation where that's actually recommended? Maybe we can skip this backward compatibility and document explicitly how to mount stuff below the root path? There's a commented block in TestChiRouterPrefix that shows the two possible variants.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant