Skip to content

Commit

Permalink
Merge pull request #656 from lucaspopp-wbd/main
Browse files Browse the repository at this point in the history
Exclude embedded fields in response header docs
  • Loading branch information
danielgtaylor authored Nov 22, 2024
2 parents bb5d167 + affa017 commit bfe8892
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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
17 changes: 17 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,13 @@ Content of example2.txt.
func TestOpenAPI(t *testing.T) {
r, api := humatest.New(t, huma.DefaultConfig("Features Test API", "1.0.0"))

// Used to validate exclusion of embedded structs from response headers
type PaginationHeaders struct {
Link string `header:"link"`
}

type Resp struct {
PaginationHeaders
Body struct {
Greeting string `json:"greeting"`
}
Expand Down Expand Up @@ -1964,6 +1970,17 @@ 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.Contains(t, openapiBody, "link")
assert.NotContains(t, openapiBody, "PaginationHeaders")
})
}

type IntNot3 int
Expand Down

0 comments on commit bfe8892

Please sign in to comment.