Skip to content

Commit

Permalink
feat: Silence unexported fields warning
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Feb 12, 2025
1 parent 84d8aef commit 46273e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ func parseStructTags(t reflect.Type, schemaRef *openapi3.SchemaRef) {

for i := range t.NumField() {
field := t.Field(i)
if !field.IsExported() {
continue
}
if field.Anonymous {
fieldType := field.Type
parseStructTags(fieldType, schemaRef)
Expand Down
15 changes: 15 additions & 0 deletions openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,18 @@ func TestDeclareCustom200Response(t *testing.T) {
require.NotNil(t, openAPIResponse.Value.Content.Get("image/png"))
require.Equal(t, "Generated image", *openAPIResponse.Value.Description)
}

func TestPrivateFieldInStruct(t *testing.T) {
type User struct {
ID int `json:"id"`
Name string `json:"name" validate:"required,min=1,max=100" example:"Napoleon"`
password string // example of private field
}

handler := slogassert.New(t, slog.LevelWarn, nil)

s := NewServer(WithLogHandler(handler))
Post(s, "/user", func(c ContextWithBody[User]) (User, error) { return c.Body() })

handler.AssertEmpty() // No warning "Property not found in schema" for the 'password' field
}

0 comments on commit 46273e3

Please sign in to comment.