这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@fourcels
Copy link
Contributor

// GreetingOutput represents the greeting operation response.
type GreetingOutput struct {
	Body struct {
		Person Person            `json:"person"`
		Map    map[string]string `json:"map"`
	}
}

type Person struct {
	Name     string `json:"name"`
	Age      int    `json:"age,omitempty" default:"20"`
	Birthday string `json:"birthday,omitempty"`
}

func main() {
	// Create a new router & API
	router := chi.NewMux()
	api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0"))

	// Register GET /greeting
	huma.Get(api, "/greeting", func(ctx context.Context, input *struct {
		Person Person            `query:"person,deepObject"`
		Map    map[string]string `query:"map,deepObject"`
	}) (*GreetingOutput, error) {
		out := &GreetingOutput{}
		out.Body.Person = input.Person
		out.Body.Map = input.Map
		return out, nil
	})

	// Start the server!
	log.Println("http://127.0.0.1:8888/docs")
	http.ListenAndServe("127.0.0.1:8888", router)
}
curl --request GET \
  --url 'http://127.0.0.1:8888/greeting?person%5Bname%5D=foo&map%5Ba%5D=foo&map%5Bb%5D=foo2' \
  --header 'Accept: application/json, application/problem+json'

Response

{
  "$schema": "http://127.0.0.1:8888/schemas/GreetingOutputBody.json",
  "person": {
    "name": "foo",
    "age": 20
  },
  "map": {
    "a": "foo",
    "b": "foo2"
  }
}

@codecov
Copy link

codecov bot commented Jan 23, 2025

Codecov Report

Attention: Patch coverage is 98.40000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 92.90%. Comparing base (48fea39) to head (f5e7967).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
huma.go 98.40% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #711      +/-   ##
==========================================
+ Coverage   92.78%   92.90%   +0.11%     
==========================================
  Files          22       22              
  Lines        5062     5174     +112     
==========================================
+ Hits         4697     4807     +110     
- Misses        314      315       +1     
- Partials       51       52       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@danielgtaylor danielgtaylor added the enhancement New feature or request label Feb 4, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return errors.New("invalid integer")
Copy link

Copilot AI Feb 18, 2025

Choose a reason for hiding this comment

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

The error message 'invalid integer' is too generic. Consider specifying the field name in the error message.

Suggested change
return errors.New("invalid integer")
return fmt.Errorf("invalid integer for field '%s'", fieldName)

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

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

This is okay because the error gets added with additional context to the response using the PathBuffer and current value. No need to be more specific here!

@danielgtaylor danielgtaylor merged commit 68c578f into danielgtaylor:main Feb 18, 2025
5 checks passed
@fourcels fourcels deleted the feat-deepObject branch July 29, 2025 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants