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

fix: issue with default field values that can override payload field values #597

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -1293,12 +1293,22 @@
})
}
} else {
// Set defaults for any fields that were not in the input.
// Set defaults for any fields that were not in the input:
atLeastOneFieldIsOverriden := false
defaults.Every(v, func(item reflect.Value, def any) {
if item.IsZero() {
if item.IsZero() && !reflect.ValueOf(def).IsZero() {
// There is one caveat here:
// - IF the default value is not the 'zero' of the field type
// - AND the 'zero' of the field type is a valid value specified in the input
// - THEN the input required field value is overriden with a different value

Check failure on line 1303 in huma.go

View workflow job for this annotation

GitHub Actions / Build & Test (1.22)

`overriden` is a misspelling of `overridden` (misspell)
item.Set(reflect.Indirect(reflect.ValueOf(def)))
atLeastOneFieldIsOverriden = true
}
})
// Restore wrongly overriden input values if any

Check failure on line 1308 in huma.go

View workflow job for this annotation

GitHub Actions / Build & Test (1.22)

`overriden` is a misspelling of `overridden` (misspell)
if atLeastOneFieldIsOverriden {
_ = api.Unmarshal(ctx.Header("Content-Type"), body, f.Addr().Interface())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems kind of inefficient and I'm wondering if there are any edge cases where it would inadvertently overwrite something... 🤔

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I propose a different way to fix this.

}
}
}

Expand Down
5 changes: 4 additions & 1 deletion huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,17 @@
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, len(input.Body.Items), 2)

Check failure on line 630 in huma_test.go

View workflow job for this annotation

GitHub Actions / Build & Test (1.22)

len: use assert.Len (testifylint)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use assert.Len for slice length assertion

On line 630, it's more expressive to use assert.Len when checking the length of a slice. This provides better error messages on failure.

Apply this diff:

-	assert.Equal(t, len(input.Body.Items), 2)
+	assert.Len(t, input.Body.Items, 2)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert.Equal(t, len(input.Body.Items), 2)
assert.Len(t, input.Body.Items, 2)
🧰 Tools
🪛 GitHub Check: Build & Test (1.22)

[failure] 630-630:
len: use assert.Len (testifylint)

assert.Equal(t, 1, input.Body.Items[0].ID)
assert.True(t, input.Body.Items[0].Verified)
assert.Equal(t, 1, input.Body.Items[1].ID)
assert.False(t, input.Body.Items[1].Verified)
return nil, nil
})
},
Method: http.MethodPut,
URL: "/body",
Body: `{"items": [{"id": 1}]}`,
Body: `{"items": [{"id": 1}, {"id": 1, "verified": false}]}`,
},
{
Name: "request-body-required",
Expand Down
Loading