Repository: danielgtaylor/huma
Pull request #592 (2024-09-30 13:39:00)

fix: panic - allow for parameters to be subtype of string
Huma panics when query param is slice of string subtypes 

for example 
![obrazek](https://github.com/user-attachments/assets/4811e80a-2b56-4d5a-8d9f-6c79500ab88f)

results in panic 

```
reflect.Set: value of type []string is not assignable to type []enum.DeviceEvent
goroutine 35 [running]:
net/http.(*conn).serve.func1()
	C:/Software/go/go1.22.5/src/net/http/server.go:1903 +0x13f
panic({0x125ba40?, 0xc00046e440?})
	C:/Software/go/go1.22.5/src/runtime/panic.go:770 +0x136
reflect.Value.assignTo({0x124da20, 0xc0005101f8, 0x97}, {0x1375748, 0xb}, 0x124a5a0, 0x0)
	C:/Software/go/go1.22.5/src/reflect/value.go:3356 +0x4b1
reflect.Value.Set({0x124a5a0, 0xc00050c998, 0x197}, {0x124da20, 0xc0005101f8, 0x97})
	C:/Software/go/go1.22.5/src/reflect/value.go:2325 +0xcf
github.com/danielgtaylor/huma/v2.Register[...].func1.2(0xc0002c3420)
	C:/Users/hlavacekv/go/pkg/mod/github.com/danielgtaylor/huma/v2@v2.23.0/huma.go:953 +0x2bbb
```

This pullrequest is a fix to that. First time i used golang reflection so feel free to clean it up a bit. 

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
	- Enhanced API parameter handling for complex data types and multipart requests.
	- Introduced a new custom type for testing, improving test coverage for middleware and parameter scenarios.

- **Bug Fixes**
	- Improved error handling and validation for request bodies and parameters, ensuring more robust API responses.

- **Tests**
	- Expanded test cases to include new scenarios for cookies, multipart uploads, and nested resolvers, enhancing overall test coverage and clarity.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

coderabbitai[bot]: <!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- This is an auto-generated comment: summarize review in progress by coderabbit.ai -->

> [!NOTE]
> Currently processing new changes in this PR. This may take a few minutes, please wait...
> 
> 	<details>
> <summary>📥 Commits</summary>
> 
> Files that changed from the base of the PR and between 8313d665358bf42eab9283a15892f51d1ea49d52 and 420c69f43c3160375004a4823848eb482980bb2e.
> 
> </details>
> 
> <details>
> <summary>📒 Files selected for processing (2)</summary>
> 
> * huma.go (1 hunks)
> * huma_test.go (5 hunks)
> 
> </details>
> 
> 	```ascii
>  __________________________________________________________
> < Keep your friends close, but your code reviewers closer. >
>  ----------------------------------------------------------
>   \
>    \   (\__/)
>        (•ㅅ•)
>        / 　 づ
> ```

<!-- end of auto-generated comment: summarize review in progress by coderabbit.ai --><!-- usage_tips_start -->

> [!TIP]
> <details>
> <summary>CodeRabbit can generate a title for your PR based on the changes.</summary>
> 
> Add @coderabbitai placeholder anywhere in the title of your PR and CodeRabbit will replace it with a title based on the changes in the PR. You can change the placeholder by changing the `reviews.auto_title_placeholder` setting.
> 
> </details>

<!-- usage_tips_end --><!-- tips_start -->

---

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Generate unit testing code for this file.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai generate unit testing code for this file.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and generate unit testing code.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### CodeRabbit Configuration File (`.coderabbit.yaml`)

- You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository.
- Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json`

### Documentation and Community

- Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->
hlavavit: Bot saved me wrong merge removed that one line i next commit

Patch:
diff --git a/huma.go b/huma.go
index 9ef44a7..ce78d76 100644
--- a/huma.go
+++ b/huma.go
@@ -950,7 +950,18 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
 						switch f.Type().Elem().Kind() {
 
 						case reflect.String:
-							f.Set(reflect.ValueOf(values))
+							if f.Type() == reflect.TypeOf(values) {
+								f.Set(reflect.ValueOf(values))
+							} else {
+								//Change element type to support slice of string subtypes (enums)
+								enumValues := reflect.New(f.Type()).Elem()
+								for _, val := range values {
+									enumVal := reflect.New(f.Type().Elem()).Elem()
+									enumVal.SetString(val)
+									enumValues.Set(reflect.Append(enumValues, enumVal))
+								}
+								f.Set(enumValues)
+							}
 							pv = values
 
 						case reflect.Int:


Test patch:
diff --git a/huma_test.go b/huma_test.go
index 15240e5..7fa0f8d 100644
--- a/huma_test.go
+++ b/huma_test.go
@@ -69,6 +69,8 @@ type BodyContainer struct {
 	}
 }
 
+type CustomStringParam string
+
 func TestFeatures(t *testing.T) {
 	for _, feature := range []struct {
 		Name         string
@@ -345,24 +347,26 @@ func TestFeatures(t *testing.T) {
 					Method: http.MethodGet,
 					Path:   "/test-params/{string}/{int}/{uuid}",
 				}, func(ctx context.Context, input *struct {
-					PathString   string    `path:"string" doc:"Some docs"`
-					PathInt      int       `path:"int"`
-					PathUUID     UUID      `path:"uuid"`
-					QueryString  string    `query:"string"`
-					QueryInt     int       `query:"int"`
-					QueryDefault float32   `query:"def" default:"135" example:"5"`
-					QueryBefore  time.Time `query:"before"`
-					QueryDate    time.Time `query:"date" timeFormat:"2006-01-02"`
-					QueryURL     url.URL   `query:"url"`
-					QueryUint    uint32    `query:"uint"`
-					QueryBool    bool      `query:"bool"`
-					QueryStrings []string  `query:"strings"`
-					QueryInts    []int     `query:"ints"`
-					QueryInts8   []int8    `query:"ints8"`
-					QueryInts16  []int16   `query:"ints16"`
-					QueryInts32  []int32   `query:"ints32"`
-					QueryInts64  []int64   `query:"ints64"`
-					QueryUints   []uint    `query:"uints"`
+					PathString         string              `path:"string" doc:"Some docs"`
+					PathInt            int                 `path:"int"`
+					PathUUID           UUID                `path:"uuid"`
+					QueryString        string              `query:"string"`
+					QueryCustomString  CustomStringParam   `query:"customString"`
+					QueryInt           int                 `query:"int"`
+					QueryDefault       float32             `query:"def" default:"135" example:"5"`
+					QueryBefore        time.Time           `query:"before"`
+					QueryDate          time.Time           `query:"date" timeFormat:"2006-01-02"`
+					QueryURL           url.URL             `query:"url"`
+					QueryUint          uint32              `query:"uint"`
+					QueryBool          bool                `query:"bool"`
+					QueryStrings       []string            `query:"strings"`
+					QueryCustomStrings []CustomStringParam `query:"customStrings"`
+					QueryInts          []int               `query:"ints"`
+					QueryInts8         []int8              `query:"ints8"`
+					QueryInts16        []int16             `query:"ints16"`
+					QueryInts32        []int32             `query:"ints32"`
+					QueryInts64        []int64             `query:"ints64"`
+					QueryUints         []uint              `query:"uints"`
 					// QueryUints8   []uint8   `query:"uints8"`
 					QueryUints16  []uint16    `query:"uints16"`
 					QueryUints32  []uint32    `query:"uints32"`
@@ -381,6 +385,7 @@ func TestFeatures(t *testing.T) {
 					assert.Equal(t, 123, input.PathInt)
 					assert.Equal(t, UUID{UUID: uuid.MustParse("fba4f46b-4539-4d19-8e3f-a0e629a243b5")}, input.PathUUID)
 					assert.Equal(t, "bar", input.QueryString)
+					assert.Equal(t, CustomStringParam("bar"), input.QueryCustomString)
 					assert.Equal(t, 456, input.QueryInt)
 					assert.InDelta(t, 135, input.QueryDefault, 0)
 					assert.True(t, input.QueryBefore.Equal(time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)))
@@ -389,6 +394,7 @@ func TestFeatures(t *testing.T) {
 					assert.EqualValues(t, 1, input.QueryUint)
 					assert.True(t, input.QueryBool)
 					assert.Equal(t, []string{"foo", "bar"}, input.QueryStrings)
+					assert.Equal(t, []CustomStringParam{"foo", "bar"}, input.QueryCustomStrings)
 					assert.Equal(t, "baz", input.HeaderString)
 					assert.Equal(t, 789, input.HeaderInt)
 					assert.Equal(t, []int{2, 3}, input.QueryInts)
@@ -416,7 +422,7 @@ func TestFeatures(t *testing.T) {
 				assert.Equal(t, "string", api.OpenAPI().Paths["/test-params/{string}/{int}/{uuid}"].Get.Parameters[29].Schema.Type)
 			},
 			Method: http.MethodGet,
-			URL:    "/test-params/foo/123/fba4f46b-4539-4d19-8e3f-a0e629a243b5?string=bar&int=456&before=2023-01-01T12:00:00Z&date=2023-01-01&url=http%3A%2F%2Ffoo.com%2Fbar&uint=1&bool=true&strings=foo,bar&ints=2,3&ints8=4,5&ints16=4,5&ints32=4,5&ints64=4,5&uints=1,2&uints16=10,15&uints32=10,15&uints64=10,15&floats32=2.2,2.3&floats64=3.2,3.3&exploded=foo&exploded=bar",
+			URL:    "/test-params/foo/123/fba4f46b-4539-4d19-8e3f-a0e629a243b5?string=bar&customString=bar&int=456&before=2023-01-01T12:00:00Z&date=2023-01-01&url=http%3A%2F%2Ffoo.com%2Fbar&uint=1&bool=true&strings=foo,bar&customStrings=foo,bar&ints=2,3&ints8=4,5&ints16=4,5&ints32=4,5&ints64=4,5&uints=1,2&uints16=10,15&uints32=10,15&uints64=10,15&floats32=2.2,2.3&floats64=3.2,3.3&exploded=foo&exploded=bar",
 			Headers: map[string]string{
 				"string": "baz",
 				"int":    "789",