danielgtaylor / huma

Huma REST/HTTP API Framework for Golang with OpenAPI 3.1
https://huma.rocks/
MIT License
1.95k stars 144 forks source link

Applying common headers to multiple responses (doc's issue) #480

Open outofthisworld opened 3 months ago

outofthisworld commented 3 months ago

So i'm trying to get my head around how I can utilise common headers in all my responses. I tried the following

type CommonHeaders struct {
    MyHeader string `header:"X-My-Header"`
}

type OutputSchema struct {
    Body           *OutputBody
    *CommonHeaders `json:"-,omitempty" hidden:"true"`
}

This outputs the following in the docs:

Headers
---------------
CommonHeaders object
       MyHeader   string    required

My-Header string

As you can see the embedded pointer struct gets documented, as well as the header seperately. Even when hidden is true, and json is "-".

I can define the headers in my schema definition instead so that they could be reused across endpoints.. but I still need to be able to write the headers to the response anyway (which I need a struct for)? and since huma.register takes a context.Context rather than the huma.Context I'm struggling to see any way to write headers and have them documented correctly other than to duplicate the headers everywhere?

EDIT: Have determined it works as expected IF the struct is embedded directly, instead of a pointer.