Skip to content
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

Exclude embedded fields in response header docs #656

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ type headerInfo struct {

func findHeaders(t reflect.Type) *findResult[*headerInfo] {
return findInType(t, nil, func(sf reflect.StructField, i []int) *headerInfo {
// Ignore embedded fields
if sf.Anonymous {
return nil
}

header := sf.Tag.Get("header")
if header == "" {
header = sf.Name
Expand Down
15 changes: 15 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,12 @@ Content of example2.txt.
func TestOpenAPI(t *testing.T) {
r, api := humatest.New(t, huma.DefaultConfig("Features Test API", "1.0.0"))

type PaginationHeaders struct {
Link string `header:"link"`
}

type Resp struct {
PaginationHeaders
Body struct {
Greeting string `json:"greeting"`
}
Expand Down Expand Up @@ -1964,6 +1969,16 @@ func TestOpenAPI(t *testing.T) {

assert.Equal(t, 200, w.Code, w.Body.String())
}

t.Run("ignore-anonymous-header-structs", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "/openapi.yaml", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)

openapiBody := w.Body.String()
assert.Equal(t, 200, w.Code, openapiBody)
assert.NotContains(t, openapiBody, "PaginationHeaders")
})
}

type IntNot3 int
Expand Down
Loading