danielgtaylor/huma

Improvement: Adapters should be split into completely separate modules

Closed this issue · 2 comments

Having all adapters in same module as huma itself is a bit weird cause you currently end up pulling all of available router adapter dependencies (so bun, fiber, chi, gin, echo, httprouter). Irrelevant of those you actually use.

@23doors this should not be the case. Do you have an example of these dependencies getting pulled in?

Since Go 1.17 it automatically does dependency graph pruning, meaning anything from a package you don't use will be removed from the dependency graph and won't go into your go.mod.

Here's a basic example I just did. Given this main.go:

package main

import (
	"context"
	"net/http"

	"github.com/danielgtaylor/huma/v2"
	"github.com/danielgtaylor/huma/v2/adapters/humago"
)

// GreetingOutput represents the greeting operation response.
type GreetingOutput struct {
	Body struct {
		Message string `json:"message" example:"Hello, world!" doc:"Greeting message"`
	}
}

func main() {
	mux := http.NewServeMux()
	api := humago.New(mux, huma.DefaultConfig("My API", "1.0.0"))

	huma.Get(api, "/greeting/{name}", func(ctx context.Context, input *struct {
		Name string `path:"name" maxLength:"30" example:"world" doc:"Name to greet"`
	}) (*GreetingOutput, error) {
		resp := &GreetingOutput{}
		resp.Body.Message = "Hello, " + input.Name + "!"
		return resp, nil
	})

	http.ListenAndServe(":8000", mux)
}

Running go mod init github.com/test or go mod tidy gives me this go.mod file with only a single dependency:

module github.com/test

go 1.23.3

require github.com/danielgtaylor/huma/v2 v2.26.0

Also check out this related article: https://dgt.hashnode.dev/reducing-go-dependencies

You're right, I must have missed graph pruning functionality. And seeing some complaints on reddit about huma dependencies I didnt properly verify it myself.

Verified with go mod vendor and go mod download in clean environment - unused packages aren't pulled in and aren't a part of go.mod/go.sum.

Closing!