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

When the tag default is used in a bool field huma is ignoring the sent value #627

Closed
robsonpeixoto opened this issue Oct 25, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@robsonpeixoto
Copy link

{
	Name: "request-body-defaults-respect-input",
	Register: func(t *testing.T, api huma.API) {
		huma.Register(api, huma.Operation{
			Method: http.MethodPut,
			Path:   "/body",
		}, func(ctx context.Context, input *struct {
			Body struct {
				// Test defaults for primitive types.
				Name  string `json:"name,omitempty" default:"Huma"`
				Count int    `json:"count,omitempty" default:"5"`
				// Test defaults for slices of primitives.
				Tags    []string `json:"tags,omitempty" default:"foo, bar"`
				Numbers []int    `json:"numbers,omitempty" default:"[1, 2, 3]"`
				// Test defaults for fields within slices of structs.
				Items []struct {
					ID       int  `json:"id"`
					Verified bool `json:"verified,omitempty" default:"true"`
				} `json:"items,omitempty"`
			}
		},
		) (*struct{}, error) {
			assert.Equal(t, "Huma", input.Body.Name)
			assert.Equal(t, 5, input.Body.Count)
			assert.Equal(t, []string{"foo", "bar"}, input.Body.Tags)
			assert.Equal(t, []int{1, 2, 3}, input.Body.Numbers)
			assert.Equal(t, 1, input.Body.Items[0].ID)
			assert.False(t, input.Body.Items[0].Verified)
			return nil, nil
		})
	},
	Method: http.MethodPut,
	URL:    "/body",
	Body:   `{"items": [{"id": 1, "verified": false}]}`,
},

For the body {"items": [{"id": 1, "verified": false}]}, the field Verified should be false, but the default:"true" is forcing the value to true.

@barata0
Copy link

barata0 commented Oct 27, 2024

It is not only with bool fields.
But every field that responds true to IsZero() (eg. Numeric Zero count=0) will be overridden by defaults.
This PR fixes this

@danielgtaylor
Copy link
Owner

Fixed in #633.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants