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

Allow providing name hints using tags for anonymous body structs #416

Merged
merged 2 commits into from
Apr 28, 2024

Conversation

lsdch
Copy link
Contributor

@lsdch lsdch commented Apr 26, 2024

Small change introducing the possibility to provide name hints for request/response body through struct tags. Previously discussed in #322

Example:

type EndpointInput struct {
  Body struct {
    SomeData string `json:"some_data"`
  } `name-hint:"SomeName"`
}

Because it defines a name hint, it only applies to anonymous types. We could go further and allow overriding the name altogether but that would involve changing the schema namer interface (func (t reflect.Type, hint string) string) to include one more parameter, which might break backward compatibility.

Copy link

codecov bot commented Apr 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.73%. Comparing base (ed2c3cd) to head (f2d014e).
Report is 9 commits behind head on main.

❗ Current head f2d014e differs from pull request most recent head 831b1db. Consider uploading reports for the commit 831b1db to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #416      +/-   ##
==========================================
+ Coverage   92.65%   92.73%   +0.08%     
==========================================
  Files          21       21              
  Lines        3539     3552      +13     
==========================================
+ Hits         3279     3294      +15     
+ Misses        221      220       -1     
+ Partials       39       38       -1     

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

@lsdch lsdch force-pushed the body-name-hints branch from 45cf03b to 7ab1280 Compare April 26, 2024 18:42
Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

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

This is awesome, thank you!

huma.go Outdated

s := SchemaFromField(registry, f, getHint(inputType, f.Name, op.OperationID+"Request"))
hint := getHint(inputType, f.Name, op.OperationID+"Request")
if nameHint := f.Tag.Get("name-hint"); nameHint != "" {
Copy link
Owner

Choose a reason for hiding this comment

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

One small nit: our existing tags (e.g. see https://huma.rocks/features/request-validation/#validation-tags) use lowerCamelCasing so we should probably use nameHint for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright ! Changed it to nameHint

@lsdch lsdch force-pushed the body-name-hints branch from 7ab1280 to f2d014e Compare April 28, 2024 12:22
@danielgtaylor danielgtaylor merged commit 68006f5 into danielgtaylor:main Apr 28, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants